Jump to content

Using ObjectDBX to copy Layers from one drawing to other


johnbest

Recommended Posts

Hi,

 

I am trying to copy a layer "DATE" from an old drawing file and then pasting to new drawing file. Following is the  VBA code...

 

Private Sub CommandButton1_Click()
    Dim oDoc As New AxDbDocument
    Dim nDoc As New AxDbDocument
    Dim newPath As String   'without layer and updated
    Dim oldPath As String   'to update
    
    UserForm1.Hide
    'Call the copyLayer()
    CopyLayers newPath, oldPath
End Sub

Private Sub UserForm_Activate()
    oldPath = "E:\ACE\Testing\09Dec\oldFile.dwg"
    newPath = "E:\ACE\Testing\30Jan\newFile.dwg"
    savPath = "E:\ACE\Testing\09Dec\oldFile_Copy.dwg"
End Sub

'To copy the Layers from newFile and pasting them to OldFile
Public Sub CopyLayers(ByVal newFile As String, ByVal oldFile As String)
    Dim oDbx As Object          'To update i.e OLD FILE
    Dim nDbx As Object          'Updated   i.e NEW FILE
    Dim olayer As AcadLayer
    Dim i As Integer
    With ThisDrawing.Application
        Set nDbx = .GetInterfaceObject("ObjectDBX.AxDbDocument.18")
    End With
    nDbx.Open FileName:=newFile
    
    'Copying the Layers from Updated File i.e NEW FILE
    Dim copyLay() As Object
    For Each olayer In nDbx.Layers
        If UCase(olayer.Name) = "DATE" Then
            ReDim Preserve copyLay(i)
            Set copyLay(i) = olayer
            'MsgBox olayer.Name
            i = i + 1
        End If
    Next
    'MsgBox i
    Dim idPairs As Variant
    'Dim copyObj As Variant
    
    'Opening the NEW FILE to copy the Layer
    With ThisDrawing.Application
        Set oDbx = .GetInterfaceObject("ObjectDBX.AxDbDocument.18")
    End With
    oDbx.Open FileName:=oldFile
    nDbx.CopyObjects copyLay, ThisDrawing.Application.ActiveDocument.ModelSpace, idPairs
    oDbx.SaveAs savPath
    Set oDbx = Nothing
    Set nDbx = Nothing
End Sub

 

There no error, but my old file is not getting updated.. !! Whats wrong with this code???

 

Regards

JB

Link to comment
Share on other sites

Just get a copy of Steal.lsp from www.Lee-mac.com does more than layers, can be command line driven so no user interaction, use (if (not steal)(load "StealV1-8)) to demand load the program a simple 1 line in your code.

  • Like 2
Link to comment
Share on other sites

9 hours ago, BIGAL said:

Just get a copy of Steal.lsp from www.Lee-mac.com does more than layers, can be command line driven so no user interaction, use (if (not steal)(load "StealV1-8)) to demand load the program a simple 1 line in your code.

 

Dear BIGAL,

 

Thanks, first of all...

I will go through it, though, not much friendly with Lisp.

But what's the problem with my piece of code?

 

I have to do this for at least 50+ drawings, without manual intervention. 

 

Regards

JB

 

Link to comment
Share on other sites

Its not a problem to do to 50 dwgs. Use a script. I have done 100+ in one go.

 

I play around the edges of VBA, it may be like LISP once you go to the new dwg the VBA stops.

 

Have you inspected the variables to see what is inside them ?

 

Also look at scriptpro to write the script.

 

The lisp code

 

;;  The following example will attempt to import Layers: 'Layer1' & 'Layer2', 

;;  C:\My Folder\MyDrawing.dwg' into the current drawing.                                       ;;

   (if (not Steal)(load "StealV1-8"))
  (Steal "C:\\My Folder\\MyDrawing.dwg"
     '(
          (
              "Layers"
              ("Layer1" "Layer2")
          )           
  )
)
(command "-save" "C:\\My Folder\\newMyDrawing.dwg")
(command "close")

 

The script

open dwg1
(load "mysteallayer")
open dwg2
(load "mysteallayer")
opendwg3
(load "mysteallayer")

 

Link to comment
Share on other sites

On 2/20/2023 at 4:47 AM, BIGAL said:

Its not a problem to do to 50 dwgs. Use a script. I have done 100+ in one go.

 

I play around the edges of VBA, it may be like LISP once you go to the new dwg the VBA stops.

 

Have you inspected the variables to see what is inside them ?

 

 

 

 

Yes...

I have inspected the variables... 

Till, opening the NEW document, everything is as expected by me...

 

But... "copyObjects()" is not working as expected... 

 

 

Yes, I have worked with LISP, a decade ago. I will surely look into urs and Lee's code too.

 

Regards

JB

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...