Jump to content

C# net app interacting with autocad. block insertion


Recommended Posts

Posted

Hey.

 

I've been developping a application that asks the user for information that will be inserted in a autocad block.

So far i've managed to open autocad, extract coordinates and create a block with the attributes i need.

But the thing is i need to use a custom block (that's on a dwg file) and i cant figure it out how to add it to database and add the atributes i need and the placing it on the drawing. Here is some code to try and explain a bit better.

 

public void insert()
       {
           object p;
           app = OpenApp();

           app.Visible = true;

           AcadBlock block = null;
           
           p = app.ActiveDocument.Utility.GetPoint(System.Type.Missing, "Base point:");
           block = app.ActiveDocument.Blocks.Add(p, "TEST");

           block.AddAttribute(1.0, AcAttributeMode.acAttributeModeNormal, "TEST", p, "test tag", "test value");
           block.AddAttribute(1.0, AcAttributeMode.acAttributeModeNormal, "1", p, "1", "1r");
           block.AddAttribute(1.0, AcAttributeMode.acAttributeModeNormal, "2", p, "2", "2");
           block.AddAttribute(1.0, AcAttributeMode.acAttributeModeNormal, "3", p, "3", "3");
           app.ActiveDocument.Database.ModelSpace.AddMInsertBlock(p, "TEST", 1.0, 1.0,1.0, 0.0, 1, 1, 1, 1, System.Type.Missing);

 

i've started only 2 weeks ago developing for autocad so if someone could please help.

 

Thanks in advance

Posted

i've almost figure it out.

 

the missing part now is how to convert ( cast) a System.object[] to Autodesk.Autocad.interop.comom.acadAtribute[].

 

In the code below im inserting the block on the drawing and then trying to change it's attributes and then update the block. but the

bl.GetAttributes(); 

Returns a object[] and not a acadatribute [].

 

 

bl = app.ActiveDocument.ModelSpace.InsertBlock(p, "CARIMBO-DTE", 1.0, 1.0, 1.0, 1.0, System.Type.Missing);

           AcadAttribute[] att;

           object[] atr;

          att = bl.GetAttributes();

           

           //AcadAttribute[] atr;

          //atr = att;

          //att = (Autodesk.AutoCAD.Interop.Common.AcadAttribute[])atr ;

           

           for (int i = 0; i < att.Length; i++)
           {
               if (att[i].TagString == "IDEDIFICIO")
               {
                   att[i].TextString = "TESTE";
               }
               //if ((Autodesk.AutoCAD.Interop.Common.AcadAttribute[])att[i].TagString == "IDEDIFICIO")
               //{
               //    (Autodesk.AutoCAD.Interop.Common.AcadAttribute[])att[i].TextString = "TESTE";
               //}
           }

           bl.Update();

 

I'm almost there someone please any ideias?

Posted
i've figured it out.

 

thx anyway!

 

Sorry to have not seen this earlier... You might post your solution, so that others who have the same problem might get help. :thumbsup:

Posted

Hi. It's ok.

In c# i was getting trouble in converting a System.__ComObject to AcadAtribute tried several things and none worked.

So i had to go the other way around.

I've created a macro in Autocad that read from a file and then place the block on the drawing and fill it with the attributes. So now in c# i just have to call the runMacro command.

app = openApp();
           app.Visible = true;
           app.RunMacro("readInfo");

 

It's not a great option because you have to create a file for every block you want to place and then delete it but for now it will have to work. :)

 

here's the code

Sub insert(ByVal id As String, ByVal res As String)

Dim BlockRefObj As AcadBlockReference

Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(ThisDrawing.Utility.GetPoint(, "Pick location:"), "BLOCK NAME", 1#, 1#, 1#, 0)

Dim Attributes
Dim i As Integer
i = 0
Attributes = BlockRefObj.GetAttributes


For i = LBound(Attributes) To UBound(Attributes)
If Attributes(i).TagString = "ID" Then
Attributes(i).TextString = id
End If
If Attributes(i).TagString = "RES" Then
Attributes(i).TextString = res
End If
Next i


BlockRefObj.Update

Set BlockRefObj = Nothing

End Sub
Sub readInfo()
Const ForReading = 1, ForWriting = 2, ForAppending = 8, TristateFalse = 0
Dim fs, f
Dim id As String
Dim res As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\1.tmp", ForReading, TristateFalse)
Do While f.AtEndOfStream <> True

id = f.ReadLine
res = f.ReadLine
Loop
Call insert(id, res)

f.Close
End Sub

 

If someone knows how to make this happen using only c# please i'll be very gratefull.

Posted

thx.

I've been having trouble it these dll's

acdmgd

admgd

I guess they are used in the first that fist link.

i dont know why they just throw exception at runtime :S

Posted

Are you using the correct references...?

 

The ObjectARX SDK includes AcDbMgd.dll, and AcMgd.dll (among others).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...