Author Topic: Looking for some example code to insert block. (C#)  (Read 13078 times)

0 Members and 1 Guest are viewing this topic.

TR

  • Guest
Looking for some example code to insert block. (C#)
« on: July 27, 2005, 11:42:57 AM »
If anyone has any example code for inserting a block from a file can you post it here? I am squirming my way through now but something to go by would be greatly appreciated.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Looking for some example code to insert block. (C#)
« Reply #1 on: July 27, 2005, 05:28:51 PM »
Hi Tim,

... a bit more than you wanted ...

This is not original .. I don't know the author


Code: [Select]

using System;
using System.Collections;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Interop;

namespace ClassLibrary1
{
public class DatabaseExample
{
//Insert an external file as a block definition via .NET
[CommandMethod("CopyBlock")]
public void InsertAsBlock()
{
            string sourceFileName = @"C:\BlockToCopy.dwg";

try
{
using(Database sourceDatabase = GetDatabaseFromFile(sourceFileName))
{
//Insert the blocks database into a new Block Table Record
                    HostApplicationServices.WorkingDatabase.Insert("BlockToCopy", sourceDatabase, false);
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
string errorMessage = GetMessageFromErrorStatus(e.ErrorStatus);
MessageBox.Show(errorMessage, "Copy Block Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


//Copy a block definition from an external file via ActiveX
[CommandMethod("CopyBlockX")]
public void CopyBlockActiveX ()
{
string sourceFileName = @"C:\OftenUsedBlocks.dwg";

string blockName = "ActiveX";

IAxDbDocument dbxDoc = null;

AcadDatabase activeDocument = (AcadDatabase)HostApplicationServices.WorkingDatabase.AcadDatabase;

try
{
//Open the drawing file
dbxDoc = new AxDbDocumentClass();
dbxDoc.Open(sourceFileName, null);

//Place the selected block definition in an array,
//to prepare it for copying
AcadBlock blockDef = dbxDoc.Blocks.Item(blockName);
AcadBlock[] objectsToCopy = {blockDef};

object idPair = null;

//Copy the block definition into the active drawing
dbxDoc.CopyObjects(objectsToCopy, activeDocument.Blocks, ref idPair);
}
catch{}
finally
{
dbxDoc = null;
}
}


#region Private Methods

private Database GetDatabaseFromFile(string fileName)
{
//create a blank database
Database databaseFromFile = new Database(false, true);

//read the database from file into the blank database
databaseFromFile.ReadDwgFile(fileName, System.IO.FileShare.None, false, null);

return databaseFromFile;
}

private string GetMessageFromErrorStatus(int errorCode)
{
return Enum.GetName(typeof(ErrorStatus), errorCode);
}

#endregion
}
}
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TR

  • Guest
Looking for some example code to insert block. (C#)
« Reply #2 on: August 06, 2005, 05:57:42 PM »
How did I miss your reply?!?!?

Thanks Kerry, I'll give it a shot when I get to work on Monday.

TR

  • Guest
Looking for some example code to insert block. (C#)
« Reply #3 on: August 11, 2005, 03:25:08 PM »
I am having trouble with the code you posted. It should be easier to understand if I just post a screenshot. Can someone tell me what I'm doing wrong with the errors on 208 and 215? Also regarding the error on line 210, is this something added in 2006 maybe? I can't seem to figure it out with 2005.

Thanks.

http://www.theswamp.org/screens/tjr/CSharpError.png

Glenn R

  • Guest
Looking for some example code to insert block. (C#)
« Reply #4 on: August 12, 2005, 10:21:56 AM »
Compare what Kerry posted to what you have in that screenshot...it is NOT the same on some fundamental levels.

TR

  • Guest
Looking for some example code to insert block. (C#)
« Reply #5 on: August 12, 2005, 10:47:32 AM »
Found it. I had added "static" to my method. Thanks.

Now the only error I have is error CS0246: The type or namespace name 'HostApplicationServicesWorkingDatabase' could not be found (are you missing a using directive or an assembly reference?) Can someone give me a tip on what I'm doing wrong with this? All my using directives contain what's in Kerry's code and I have acmgd.dll and acdbmgd.dll referenced. Is this something that's not available in 2005's .NET API?

JCappiello

  • Guest
Looking for some example code to insert block. (C#)
« Reply #6 on: August 12, 2005, 01:00:45 PM »
I think there is a typing error:

It should read  "HostApplicationServices.WorkingDatabase"

The period is missing.

Hope this helps.

Anonymous

  • Guest
Looking for some example code to insert block. (C#)
« Reply #7 on: August 12, 2005, 01:23:23 PM »
That was it. Thank you sir.

HD

  • Guest
Re: Looking for some example code to insert block. (C#)
« Reply #8 on: October 28, 2005, 08:05:40 AM »
Hello,


I was wondering if anyone had problems compiling the program that Kerry posted. I receive the following error messages during compilation:

* The best overloaded method match for 'ClassLibrary1.DatabaseExample.GetMessageFromErrorStatus(int)' has some invalid arguments

* Argument '1': cannot convert from 'Autodesk.AutoCAD.Runtime.ErrorStatus' to 'int'

Both of these messages are coming from the following line: The above messages are coming from the following line: string errorMessage = GetMessageFromErrorStatus(e.ErrorStatus);


Thanks!
« Last Edit: October 28, 2005, 08:36:12 AM by HD »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Looking for some example code to insert block. (C#)
« Reply #9 on: October 28, 2005, 02:55:34 PM »
Hi HD,

I'll have another look when I get into work mode.

Just a couple of things :-

Due to the evolving nature of the application of .NET technology it's usually handy to provide some additional information :

Which IDE are you using  ?
Which IDE's  do you have installed ?

Which .NET version are you using ?
Which .NET versions do you have installed ?

Which ACAD version are you using ?
Which ACAD versions do you have installed ?

Though this is a compile issue, the last 2 are required because of the library references

In an ideal world these perhaps wouldn't be considerations, but .....

Regards
Kerry

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Looking for some example code to insert block. (C#)
« Reply #10 on: October 28, 2005, 08:10:28 PM »
HD,
Using MSC#2003 with AC2006

with this Change to the posted Code ;
Code: [Select]
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
string errorMessage = GetMessageFromErrorStatus(Convert.ToInt32(e.ErrorStatus));
MessageBox.Show(errorMessage,
"Copy Block Error - Big dummy Spit ",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

The Assembly compiles and runs.
As near as I can determine the enumerated value returned from (e.ErrorStatus) produced by AutoCad needs to be converted to an int ie: Int32


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Looking for some example code to insert block. (C#)
« Reply #11 on: October 28, 2005, 09:04:02 PM »
.. This works as well, and probably easier :)

Code: [Select]
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
string errorMessage = (e.ErrorStatus).ToString();

MessageBox.Show(errorMessage,
"Copy Block Error - Big dummy Spit ",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Looking for some example code to insert block. (C#)
« Reply #12 on: October 28, 2005, 09:35:50 PM »
.. or if you're into frugality
Code: [Select]
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
MessageBox.Show((e.ErrorStatus).ToString(),
"Copy Block Error - Big dummy Spit ",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Looking for some example code to insert block. (C#)
« Reply #13 on: October 30, 2005, 04:29:08 PM »
Afterthought

The last 2 solutions will allow you to remove the
private string GetMessageFromErrorStatus(int errorCode) { ... } Method





[edit : DUH ! need coffee]
« Last Edit: October 30, 2005, 05:23:45 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

HD

  • Guest
Re: Looking for some example code to insert block. (C#)
« Reply #14 on: October 31, 2005, 06:26:08 AM »
Kerry,


Thanks to your post, my assembly compiles and runs.


Sorry for not posting additional information. For what it's worth, here's my environment:

Visual Studio .NET 2003
AutoCAD 2006


Thanks Again!