VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy Pasting object from one drawing file to another

27 REPLIES 27
SOLVED
Reply
Message 1 of 28
Anonymous
8080 Views, 27 Replies

Copy Pasting object from one drawing file to another

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...

27 REPLIES 27
Message 2 of 28
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Message 3 of 28
mruu88
in reply to: 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? 

Message 4 of 28
Ed.Jobe
in reply to: mruu88

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.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 5 of 28
mruu88
in reply to: Ed.Jobe

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...

Message 6 of 28
Ed.Jobe
in reply to: mruu88

If you've made changes, show your code.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 7 of 28
mruu88
in reply to: Ed.Jobe

I haven't made any changes 😕  Is there a particular reference that needs to be ticked in order to run this??

Message 8 of 28
Ed.Jobe
in reply to: mruu88

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.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 9 of 28
mruu88
in reply to: Ed.Jobe

****. Ok I had that one already selected. I'll have another fiddle today and see how i go. Thanks for your help.

Message 10 of 28
mruu88
in reply to: Ed.Jobe

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??

Message 11 of 28
Ed.Jobe
in reply to: mruu88

It's possible that  your project may have an ambiguity on "application". Try changing the following line to:

For Each d In AcadApplication.Documents

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 12 of 28
RICVBA
in reply to: Ed.Jobe

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

 

 

Message 13 of 28
Ed.Jobe
in reply to: RICVBA


@RICVBA wrote:

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 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?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 14 of 28
RICVBA
in reply to: Ed.Jobe

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 ?

Message 15 of 28
Ed.Jobe
in reply to: RICVBA


@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


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 16 of 28
mruu88
in reply to: Ed.Jobe


@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!

Message 17 of 28
mruu88
in reply to: norman.yuan

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
Message 18 of 28
Ed.Jobe
in reply to: mruu88

Yes you can select all. Look in the help under SelectionSet.Select. Use the acselectionsetAll mode.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 19 of 28
jtrojniak
in reply to: Ed.Jobe

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?

 

copying.png

Message 20 of 28
Ed.Jobe
in reply to: jtrojniak

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.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost