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.
Hi,
>>>there<<< you find a sample for .NET (I would prefere that instead of COM)
- alfred -
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 -
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 -
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 -
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.
Just add some test codes.
you may copy aaa.dwg to c:\, or just change the location for the dwg file.
thanks.
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 -
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.
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.