How to copy blocks using VB.NET through COM?

How to copy blocks using VB.NET through COM?

Anonymous
Not applicable
2,567 Views
12 Replies
Message 1 of 13

How to copy blocks using VB.NET through COM?

Anonymous
Not applicable

Hi, all:

 

I got a question about access AUTOCAD through COM.

Previously I have one VB6 program, which read blocks from an external dwg files and insert into AUTOCAD2010's activepage.

Now I want to upgrade to VB.NET2010, but found one big problem cannot be resolved, that is, how to copy blocks from an external dwg files?


I use the command below to copy blocks from dwg file:

Call GetBlocksFromDwg("d:\abc.dwg", obj_Doc)

obj_'s definition is:

Dim obj_Doc As Autodesk.AutoCAD.Interop.AcadDocument


Previous VB6 source codes:

Private Sub GetBlocksFromDwg(DwgName As String, Target As Object)

Set DbxDoc = obj_Acad.GetInterfaceObject("ObjectDBX.AxDbDocument.18")

DbxDoc.Open DwgName

Dim Objects(0 To 0) As Object

For Each entry In DbxDoc.Blocks

    Set Objects(0) = DbxDoc.Blocks(entry.Name)

    DbxDoc.CopyObjects Objects, Target.Blocks

Next

End Sub

Current VB.NET 2010's source codes:

Private Sub GetBlocksFromDwg(ByRef DwgName As String, ByRef Target As Object)

Dim entry As Object

Dim DbxDoc As Object

DbxDoc = obj_Acad.GetInterfaceObject("ObjectDBX.AxDbDocument.18")

DbxDoc.Open(DwgName)

Dim Objects(0) As Object
For Each entry In DbxDoc.Blocks

  Objects(0) = DbxDoc.Blocks(entry.Name)

  CopyObjects(Objects, Target.Blocks, Type.Missing)

Next entry

End Sub

 

The command below cannot work:

CopyObjects(Objects, Target.Blocks, Type.Missing)

 

Please advise, thanks a lot.

 

0 Likes
2,568 Views
12 Replies
Replies (12)
Message 2 of 13

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>>>there<<< you find a sample for .NET (I would prefere that instead of COM)

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 13

Anonymous
Not applicable

Hi, Alfred:

 

The problem is I composed an application EXE to access AutoCAD, so COM is the only method.

0 Likes
Message 4 of 13

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

my method of an EXE would be to just start AutoCAD, then load my DLL and let the DLL do the work 😉

But let's see if someone is coming with a ready COM-code.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 13

Anonymous
Not applicable

Yeah, hoping someone can resolve the problem.

0 Likes
Message 6 of 13

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> The command below cannot work:

>> CopyObjects(Objects, Target.Blocks, Type.Missing)

Have not tried that, but why did you change the VB6-Code from that line when converting?

DbxDoc.CopyObjects(Objects, Target.Blocks, Type.Missing)

 

What error message do you get?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 7 of 13

Anonymous
Not applicable

error message: invalid object array

0 Likes
Message 8 of 13

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

you get this message with both versions of code?

DbxDoc.CopyObjects Objects, Target.Blocks

CopyObjects(Objects, Target.Blocks, Type.Missing)

 

BTW: what version of AutoCAD do you use?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 9 of 13

Anonymous
Not applicable

DbxDoc.CopyObjects Objects, Target.Blocks

is converted to

DbxDoc.CopyObjects(Objects, Target.Blocks)

 

and

CopyObjects(Objects, Target.Blocks, Type.Missing) is illegal.

 

I use AutoCAD 2010, with VB.net 2010.

 

 

 

0 Likes
Message 10 of 13

Anonymous
Not applicable

Just add some test codes.

you may copy aaa.dwg to c:\, or just change the location for the dwg file.

thanks.

0 Likes
Message 11 of 13

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

I tried it that way and it worked (with a button in the form), I hope for you too 😉

 

   Const tSourceFileName As String = "C:\TEMP\X01.DWG"
   Const tDestFileName As String = "C:\TEMP\X02.DWG"

   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

      Dim obj_Acad As AcadApplication = CType(GetObject(, "AutoCAD.Application.18"), AcadApplication)
      Dim TargetDoc As AcadDocument = obj_Acad.Documents.Open(tDestFileName)

      Dim DbxDoc As Object = obj_Acad.GetInterfaceObject("ObjectDBX.AxDbDocument.18")
      DbxDoc.Open(tSourceFileName)

      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)
            Debug.Print(entry.Name)
         End If
      Next entry

   End Sub

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 12 of 13

Anonymous
Not applicable

Hi, Alfred:

 

Thanks a lot, it works.

The key is:

dim entry as acadblock

dim Objects(0) as acadblock

 

Objects(0)=entry

DbxDoc.CopyObjects(CObj(Objects), TargetDoc.Blocks)

 

Fantastic!

 

Thanks.

0 Likes
Message 13 of 13

murugasubi
Enthusiast
Enthusiast

Hi. I am trying to copy a entity from one document to another. but not copying. 

This is my code.

 

Autocad:2014, .net-2010

 

 

Public AcadApp As AutoCAD.AcadApplication
Public SourceDoc As AutoCAD.AcadDocument
Public DestDoc As AutoCAD.AcadDocument

 

AcadApp = CreateObject("AutoCAD.Application")

 

Dim circleObj1 As AutoCAD.AcadCircle
Dim circleObj2 As AutoCAD.AcadCircle

Dim centerPoint(0 To 2) As Double

Dim objCollection(0 To 1) As Object

centerPoint(0) = 0.0# : centerPoint(1) = 0.0# : centerPoint(2) = 0.0#

circleObj1 = SourceDoc.ModelSpace.AddCircle(centerPoint, 5)
circleObj2 = SourceDoc.ModelSpace.AddCircle(centerPoint, 10)

objCollection(0) = circleObj1
objCollection(1) = circleObj2

 

DestDoc = AcadApp.Documents.Add("Destination.dwg")
DestDoc = AcadApp.ActiveDocument

 

Dim destEnts As Object
destEnts = SourceDoc.CopyObjects(objCollection, DestDoc.ModelSpace)

 

'two circles drawn in soruce document- 

'destination document opend.

 

but not copying. values in destents is nothing.

 

pls. help