.NET

.NET

Reply
Valued Contributor
cncah
Posts: 77
Registered: ‎01-15-2014
Message 1 of 3 (162 Views)
Accepted Solution

How to insert an entity from another drawing using vb.net in stand-alone program

162 Views, 2 Replies
05-29-2014 04:59 AM

Hello, I'm wondering how to get a point from the user to insert an entity into a drawing from another drawing using vb.net in a stand-alone program. I'm having to write this as a stand alone program because this is only a small portion of my overall program and I'm interfacing with 3 other softwares besides Autocad. I know how to do this when writing the vb program as a dll and how to do it in AutoLisp but not as a stand alone vb program. Any help would be appreciated. Thank you.

You need to prompt the user for a point:
Dim insertionPnt = AcadDoc.Utility.GetPoint(, "Enter a point: ")

 

Then get block ref into your drawing:
Dim AcadApp As AcadApplication = CType(GetObject(, "AutoCAD.Application.?"), AcadApplication)

Dim TargetDoc As AcadDocument = AcadApp.ActiveDocument
Dim DbxDoc As Object = AcadApp.GetInterfaceObject("ObjectDBX.AxDbDocument.?")
DbxDoc.Open("the_Source_FileName")
Dim Objects(0) As Common.AcadBlock
For Each entry As Common.AcadBlock In CType(DbxDoc.Blocks, IEnumerable)
If (Not entry.IsLayout) AndAlso (Not entry.Name.StartsWith("*")) Then
Objects(0) = entry
DbxDoc.CopyObjects(CObj(Objects), TargetDoc.Blocks)
End If
Next entry

 

Then insert block into drawing at the prompted point:
AcadDoc.ModelSpace.InsertBlock(insertionPnt, "SomeBlock", 1#, 1#, 1#, 0)

 

I haven't done this in a while but I think this is all thats needed.  Maybe somebody can give you more complete code to use but if you play around with this a bit you should be able to figure it out.  Then again, I could be way off base :smileyhappy:

Distinguished Contributor
Mikko
Posts: 212
Registered: ‎10-03-2003
Message 2 of 3 (126 Views)

Re: How to insert an entity from another drawing using vb.net in stand-alone pro

05-29-2014 01:34 PM in reply to: cncah

You need to prompt the user for a point:
Dim insertionPnt = AcadDoc.Utility.GetPoint(, "Enter a point: ")

 

Then get block ref into your drawing:
Dim AcadApp As AcadApplication = CType(GetObject(, "AutoCAD.Application.?"), AcadApplication)

Dim TargetDoc As AcadDocument = AcadApp.ActiveDocument
Dim DbxDoc As Object = AcadApp.GetInterfaceObject("ObjectDBX.AxDbDocument.?")
DbxDoc.Open("the_Source_FileName")
Dim Objects(0) As Common.AcadBlock
For Each entry As Common.AcadBlock In CType(DbxDoc.Blocks, IEnumerable)
If (Not entry.IsLayout) AndAlso (Not entry.Name.StartsWith("*")) Then
Objects(0) = entry
DbxDoc.CopyObjects(CObj(Objects), TargetDoc.Blocks)
End If
Next entry

 

Then insert block into drawing at the prompted point:
AcadDoc.ModelSpace.InsertBlock(insertionPnt, "SomeBlock", 1#, 1#, 1#, 0)

 

I haven't done this in a while but I think this is all thats needed.  Maybe somebody can give you more complete code to use but if you play around with this a bit you should be able to figure it out.  Then again, I could be way off base :smileyhappy:

Valued Contributor
cncah
Posts: 77
Registered: ‎01-15-2014
Message 3 of 3 (120 Views)

Re: How to insert an entity from another drawing using vb.net in stand-alone pro

05-29-2014 02:22 PM in reply to: Mikko

That worked great! Here is what worked for me:

 

Dim acadapp As Autodesk.AutoCAD.Interop.AcadApplication = Nothing
        Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument = Nothing

        Try
            acadapp = CType(GetObject(, "AutoCAD.Application"), Autodesk.AutoCAD.Interop.AcadApplication)
            acadDoc = CType(acadapp.ActiveDocument, Autodesk.AutoCAD.Interop.AcadDocument)

            If acadapp IsNot Nothing And acadDoc IsNot Nothing Then

                Dim insertionPT = acadDoc.Utility.GetPoint(, "Pick Insertion Point Between Radii At Bottom Of Pocket:")

                Dim DbxDoc As Object = acadapp.GetInterfaceObject("ObjectDBX.AxDbDocument.19")
                DbxDoc.Open("C:\Path_To_Block_Drawing.dwg")

                Dim Objects(0) As AcadBlock
                For Each entry As AcadBlock In CType(DbxDoc.Blocks, IEnumerable)
                    If (Not entry.IsLayout) AndAlso (Not entry.Name.StartsWith("*")) Then
                        Objects(0) = entry
                        DbxDoc.CopyObjects(CObj(Objects), acadDoc.Blocks)
                    End If
                Next entry

                acadDoc.ModelSpace.InsertBlock(insertionPT, "NameOfBlock", 1.0#, 1.0#, 1.0#, 0)                
            End If

        Catch InsertBlockError As Exception
            Debug.WriteLine(InsertBlockError)
        End Try

 Thank You Very Much! I was starting to write a bunch of small AutoLisp programs that I would start with:

 

acadDoc.SendCommand("_startlispcommand ")

 

This will work much better for me and not require a bunch of AutoLisp program maintenence on our network. Thanks again!

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.