.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Code to insert a dynamic block into the current drawing

12 REPLIES 12
Reply
Message 1 of 13
kloe
7596 Views, 12 Replies

Code to insert a dynamic block into the current drawing

To whom that would be so kind.

I need simple code to do just one thing, inserting a dynamic block that is stored in a separate file on my computer (like C:/base/b-22) in the drawing that I'm currently using. For example, I have a windows form with a list box with names of many dynamic blocks that are stored on my c drive, I would like to pick a block with a name and upon a click event of a button or other will go get this block from a separate file and insert it in the drawing I'm currently using, and would need to be able to pick the point of where I want the block to go. Could someone help, I'm using autocad 2010 with visual studio 2008- using the new autocad .net language.

12 REPLIES 12
Message 2 of 13
cadMeUp
in reply to: kloe

You can try using the attached code file. It's not commented, but there are examples of usage towards the top of the file.

The public functions and properties are:

InsertFile() &

InsertBlockFromFile() &

BlockTableRecordId (gets the blocktable record id of the inserted block)

 

The class doesn't handle any of your dynamics or attributes it just gets the block def from another file into your database, you still have to go about inserting the new block into your drawing, something like:

 

Editor editor = AcadApp.DocumentManager.MdiActiveDocument.Editor;

PromptPointResult pr = editor.GetPoint(new PromptPointOptions("\nGet Point: "));

BlockReference blockRef = new BlockReference(pr.Value, insertMan.BlockTableRecordId);

 

then work on your attributes and/or dynamic properties before or after actually adding the block to your dwg, but those are relatively easy steps.

 

Note, after using the InsertFile() or InsertBlockFromFile() functions, you may have to regen the dwg.

Message 3 of 13
stevenelliott3894
in reply to: cadMeUp

cadMeUp,

 

Thanks for posting your block insert code. Since I am making the switch from VB/VBA to .Net, this will come in handy for the program I am converting now.

 

However, I used an online converter to translate your code to VB.Net and I get a couple of errors in VS2008. The first one is at the line 'Imports AcadApp = ...' and the message says "Imports alias 'AcadApp' conflicts with 'Structure AcadApp' declared in the root namespace."

 

The second one is in the line in 'Public Dub New' and begins with 'm_db = AcadApp.DocumentManager...' and the error message says, " 'AcadApp' is not accessible int this context because it is 'Friend'."

 

Now, I have no clue if this works that way in C# and not in VB, but is there any way you can tell me how to fix it?

 

 

Thanks

 

 

Steven Elliott

When I was a little bitty boy, my grandmother bought me a cute little toy. Silver bells hangin' on a string... she told me it was my ding-a-ling-a-ling!
Message 4 of 13
cadMeUp
in reply to: stevenelliott3894

Yeah, that's just one of the differences between C# and VB, I normally use C# as I find it to be easier and cleaner, to me anyway. At the top of each C# code file I usually add these statements (below my other C# "using" statements):

 

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using DatabaseServ = Autodesk.AutoCAD.DatabaseServices;
using TransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;

 

but in VB the equivalent statements would be:

 

Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports DatabaseServ = Autodesk.AutoCAD.DatabaseServices
Imports TransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager

 

So if you add those VB statements to your code file, below your other "Imports" statements, that should fix the problem.

Just give the 'AcadApp' a different name, like AcAppObject or something similar.

Hopefully you won't get the same conflicts with another namespace.

 

 

Do you have a link to the C# to VB code converter that you use? I would like to experiment with it myself, it might help me with future posts.

Message 5 of 13
stevenelliott3894
in reply to: cadMeUp

Thanks for the very prompt reply and help! I will check out the changes and let you know.

 

The converter I used is from DeveloperFusion and it is here:

 

http://www.developerfusion.com/tools/

 

This is just one of several that come up in Google, but it is clean and simple... and it works nicely (I've used it a few times).

 

 

Thanks!

Steven Elliott

When I was a little bitty boy, my grandmother bought me a cute little toy. Silver bells hangin' on a string... she told me it was my ding-a-ling-a-ling!
Message 6 of 13
Almeron
in reply to: kloe

Hi cadMeUp,

could you provide a full example to insert dynamic block through your code available?

Thanks

Message 7 of 13
KailasDhage
in reply to: Almeron

Please see attached sample code

-Kailas Dhage

Message 8 of 13
Jeffrey_H
in reply to: KailasDhage

Inserting a dynamic block would be the same as any other block, but I guess you are wanting to modify the parameters

 

Here is one

http://www.theswamp.org/index.php?topic=31859.0

You can also find your answers @ TheSwamp
Message 9 of 13
Almeron
in reply to: kloe

Jeffrey_H,
 

 

I would create the blocks parameterized linear or XY, for example, and modify them when they were inserted, which would otherwise be created for each block a block with different attributes, which is impractical for my solution.
The solution to be adopted will be to create template files dwg blocks with predefined dynamic and instantiate them by modifying them to insert code in c #. But it is a problem that many are finding as can be seen in several other forums dealing with the matter. For now I will adopt the solution above. I am studying the case.


I am very grateful for the aid.


Thanks KailasDhage the solution provided.


As you may have noticed I am new to developing AutoCAD.net in c # and my English is very bad. I will take action on it.
 

 

Hugs ...

Message 10 of 13
Almeron
in reply to: kloe

I think there's an easier solution to my problem. How can I delete a block so you can recreate it?

if (bt.Has (TestBlock "))
{
/ / Delete block?
}

Message 11 of 13
cadMeUp
in reply to: Almeron

Every DBObject has an Erase() method that you can call:

 

BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
if (bt.Has("TestBlock"))
{
 BlockTableRecord btr = (BlockTableRecord)bt["TestBlock"].GetObject(OpenMode.ForWrite);
 try
 {
  btr.Erase();
 }
 catch
 {
  ed.WriteMessage("\nException: No Erase!");
 }
}

 

You'll probably get some strange results if you have blocks that reference the BlockTableRecord that you want to erase.

Also, for pre-defined dynamic blocks you might want to look into using tool palettes, they work pretty well in that way.

Message 12 of 13
Almeron
in reply to: cadMeUp

Hello everybody!
 

 

I continue to have problems.
To simplify the example of my problem is to create a block containing only one line and a text where the line dimension is defined in the block by adding the user?
I need to add several block references with different dimensions of the line.
Thanks.

Message 13 of 13
amitnkukanur
in reply to: kloe

Hello There,

 

If in a Block "Sample" : for example

 

Sample1= D1=100,D2=200

Sample2= D1=200,D2=400

Sample3= D1=300,D2=600

 

Among these if i want to pick the last one where should i change to fetch this result.

 

 

rgds

Amit

Senior Software Engineer

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost