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

How to copy blocks using VB.NET through COM?

12 REPLIES 12
Reply
Message 1 of 13
taiven
1719 Views, 12 Replies

How to copy blocks using VB.NET through COM?

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.

 

12 REPLIES 12
Message 2 of 13
Alfred.NESWADBA
in reply to: taiven

Hi,

 

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

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 13
taiven
in reply to: taiven

Hi, Alfred:

 

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

Message 4 of 13
Alfred.NESWADBA
in reply to: taiven

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 13
taiven
in reply to: Alfred.NESWADBA

Yeah, hoping someone can resolve the problem.

Message 6 of 13
Alfred.NESWADBA
in reply to: taiven

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 7 of 13
taiven
in reply to: Alfred.NESWADBA

error message: invalid object array

Message 8 of 13
Alfred.NESWADBA
in reply to: taiven

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 9 of 13
taiven
in reply to: Alfred.NESWADBA

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.

 

 

 

Message 10 of 13
taiven
in reply to: taiven

Just add some test codes.

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

thanks.

Message 11 of 13
Alfred.NESWADBA
in reply to: taiven

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 12 of 13
taiven
in reply to: Alfred.NESWADBA

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.

Message 13 of 13
murugasubi
in reply to: taiven

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

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