Hallo everyone!
I have a problem regarding Autocad drawing with VBA coding. I am trying to copy paste an object (for ex. a circle) from one dwg file to another dwg file via VBA coding. I cant write the code for it. Is there any way to do it besides Wblock command specially when I want to copy paste objects from multiple dwg files?
Please help me...
Solved! Go to Solution.
Solved by Ed.Jobe. Go to Solution.
Solved by norman.yuan. Go to Solution.
AcadDocument.CopyObjects() method can be used to copy an array of entities to the same drawing, or another drawing that is opened in AutoCAD.
Here is a brief sample VBA code to do what you want:
Option Explicit Public Sub CopyEntities() ''Set the source drawing Dim curDwg As AcadDocument Dim d As AcadDocument For Each d In Application.Documents If UCase(d.Name) = "DRAWING1.DWG" Then Set curDwg = d Exit For End If Next If curDwg Is Nothing Then MsgBox "Cannot find source drawing ""Drawing1.dwg""" Exit Sub End If ''Draw something in the source drawing Dim entities() As Object entities = DrawSomethings(curDwg) MsgBox "Entities count=" & UBound(entities) + 1 ''Open a new drawing Dim newDwg As AcadDocument Set newDwg = Application.Documents.Add() ''Copy entities from source drawing to new drawing curDwg.CopyObjects entities, newDwg.ModelSpace End Sub Private Function DrawSomethings(dwg As AcadDocument) As Object() Dim ents() As Object ReDim ents(4) Dim ent As AcadEntity Dim i As Integer For i = 1 To 5 Set ent = DrawCircle(dwg, i) ent.Update Set ents(i - 1) = ent Next DrawSomethings = ents End Function Private Function DrawCircle(dwg As AcadDocument, i As Integer) As AcadEntity Dim centre(0 To 2) As Double centre(0) = CDbl(i + i): centre(1) = CDbl(i + i): centre(2) = 0# Dim radius As Double radius = CDbl(i * 5) Set DrawCircle = dwg.ModelSpace.AddCircle(centre, radius) End Function
Norman Yuan
''Copy entities from source drawing to new drawing curDwg.CopyObjects entities, newDwg.ModelSpace
What if you wanted to copy content from the current drawing, to the model space of another drawing already opened?
Don't let the names curDwg and newDwg confuse you. Think of it as SourceDwg and DestinationDwg. The CopyFrom method is always from the perspective of the Destination dwg. You run the method from the Destination and tell it what Source to CopyFrom.
''Copy entities from source drawing to new drawing DestinationDwg.CopyObjects entities, SourceDwg.ModelSpace
You just have to set DestinationDwg equal to the other dwg to want to copy to, and the SourceDwg equal to ThisDrawing.
Ohhh ok, got ya! Thanks.
How come I cant get past the "For Each d In Application.Documents" line?
Says the object doesn't support this method or property...
If you've made changes, show your code.
Yes, "AutoCAD 20## Type Library" should be marked, but vba does that by default. You might have a typo in your code. Norman's works fine for me.
****. Ok I had that one already selected. I'll have another fiddle today and see how i go. Thanks for your help.
Sorry, I gave it a good couple of weeks of coming back to, looking up alternative codes, incorporating parts from similar codes that are accessing CAD through VBA as well.. and it still won't seem to work for me.
I've copied it word for word - so no spelling mistakes. "AutoCAD 2017 Type Library" is ticked.
But still; "Object doesn't support this property or method".
What on earth could be missing??
It's possible that your project may have an ambiguity on "application". Try changing the following line to:
For Each d In AcadApplication.Documents
assuming upama333 is coding from within AutoCAD, he could also simply go:
For Each d In Documents
since Documents is a property of Application object, which would be implicitly resolved
@RICVBA wrote:
assuming upama333 is coding from within AutoCAD, he could also simply go:
For Each d In Documentssince Documents is a property of Application object, which would be implicitly resolved
@RICVBA The problem seems to be implicit resolution. Norman's original code should have worked also. But its not working for the OP. What may be happening is that his project references more than one library which has a "application" object and the other library's application object doesn't have a Documents collection. Explicitly specifying AcadApplication should resolve the ambiguity.
@mruu88 Can you post a screen capture of what your References dialog looks like?
Being in AutoCAD VBA and should there be another reference to a class with another Application object, wouldn't it be taken AutoCAD one for implicit resolution ?
@RICVBA wrote:
Being in AutoCAD VBA and should there be another reference to a class with another Application object, wouldn't it be taken AutoCAD one for implicit resolution ?
Apparently their system is not able to resolve the reference properly. Implicit references only work when there's no ambiguity. If there is a problem with scope, etc. you need to be explicit. Normally, there wouldn't be another conflicting reference, but its possible. Who knows what they did. That's why I asked for a screen shot. I tested Norman's code as is and it works, so they've done something out of the ordinary to break it.
@Ed.Jobe wrote:It's possible that your project may have an ambiguity on "application". Try changing the following line to:
For Each d In AcadApplication.Documents
This did it!!
There was one other instance in which I had to change the line to "AcadApplication.Documents" as well, and then it worked perfectly. Thank you so much, you put me out of my misery!
I've been trying to find a way to replace the 'entities' with 'all content from model space'.
I've stripped it back a bit - I don't need to draw anything. Is there a VBA equivalent to ssget _"X" ??
Option Explicit Public Sub CopyEntities() Dim curDwg As AcadDocument Dim d As AcadDocument For Each d In AcadApplication.Documents If UCase(d.Name) = "DRAWING1.DWG" Then Set curDwg = d Exit For End If Next If curDwg Is Nothing Then MsgBox "Cannot find source drawing ""Drawing1.dwg""" Exit Sub End If Dim newDwg As AcadDocument Set newDwg = AcadApplication.Documents.Add() curDwg.CopyObjects(ALL FROM MODEL SPACE), newDwg.ModelSpace End Sub
Yes you can select all. Look in the help under SelectionSet.Select. Use the acselectionsetAll mode.
I have tried using/editing the code from above to help with what I am trying to do, where I am trying to open up another document, copy all of its entities into the document I am currently working in and then close the other document. However, I keep getting a runtime error (438) saying that "object doesn't support this property or method". It keeps on getting stuck in the "copied" line. I know I have not closed the document being copied yet. That will come after this works.
Any thoughts?
You can't use the CopyObjects method that way. You have to pay attention to the method's signature and what type of argument it expects. The Select method doesn't return anything. It just fills a selectionset object (look at the help topic I gave you) and look at help for what arguments the CopyObjects method takes. They have to match.
The Selectionsets object doesn't have a Select method. Dim and instantiate a selectionset, use its Select method. Iterate the selectionset's items and copy them to an object array and then use that as an argument to the CopyObjects method. Use the samples in Help for guidance.