Copy polylines between dwg files using script

Copy polylines between dwg files using script

Andrzej_K.
Contributor Contributor
560 Views
7 Replies
Message 1 of 8

Copy polylines between dwg files using script

Andrzej_K.
Contributor
Contributor

Hi,

I was looking for some script to copy objects (not blocks) from multiple .dwg file to another. 

Ideally I wanted to open the destination file. Run the script and the rest will be done for me.

I have found this code:

; by alanh copy a window from another dwg
; Nov 2019
(defun openblk (blkname / adocs)
(setq acDocs (vla-get-documents (vlax-get-acad-object)))
(vla-open acDocs blkname)
(command "copyclip" "w" "13,30" "402,276" "")
(vla-close (vla-item acdocs 1) :vlax-false)
(command "pasteclip" "0,0")
)
(openblk "d:\\acadtemp\\2circs.dwg")

 But when I was running it I got some errors: 

"1; error: Automation Error. Drawing is busy."

I did some small editing (apart from changing the file address and selection range) - in the function definition it looks like there was a spelling error and "adocs" should be "acdocs" but it did not solve the issue.  

Can you please assist? 

0 Likes
561 Views
7 Replies
Replies (7)
Message 2 of 8

paullimapa
Mentor
Mentor

this thread may explain the problem and a possible solution:

https://forums.augi.com/showthread.php?165365-Open-drawing-in-Autolisp


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 8

Sea-Haven
Mentor
Mentor

That is an old one I think we just used it to open a dwg.

 

Try this as a script should work, you can write a script just use (command "script" filename)

 

(command "open" "d:\\acadtemp\\timber-edge.dwg")
(setvar 'ctab "MODEL")
(command "copyclip" "w" "13,30" "402,276" "")
close N
pasteclip "0,0"
0 Likes
Message 4 of 8

Andrzej_K.
Contributor
Contributor

Seems simple enough but when I use "open" I have a popup window from our database software which stops the process so in this case it will not work. 

0 Likes
Message 5 of 8

Sea-Haven
Mentor
Mentor

Try this sounds like Open has been redefined. This should work.

 

(command "._open" "d:\\acadtemp\\timber-edge.dwg")

  

0 Likes
Message 6 of 8

andkal
Collaborator
Collaborator

Some time ago I wrote this program that copies objects between sessions.
http://autolisps.blogspot.com/p/copybetweensessions.html 
Maybe you will find it interresting. It works with two opened sessions but I you need it to work with more than 3 sessions you can adjust it in code yourself.

pozdr.
Andrzej K.


• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
0 Likes
Message 7 of 8

Andrzej_K.
Contributor
Contributor

Hi guys, thanks for the answers.

@Sea-Haven - I still need to figure out the .dwg issue in your code (AutoCAD struggles with it "Unknown command "DWG".")

@andkal - I would need to take a deeper look. This would require me to open each document manually and run the command separately for each which is something I'd like to avoid. 

 

In the end I have found some code here that does more less what I need. 

https://www.cadtutor.net/forum/topic/70092-copy-all-object-from-another-drawing-to-the-current-drawi...

0 Likes
Message 8 of 8

Andrzej_K.
Contributor
Contributor

I am using the code I have mentioned and I run into the problem. 

It works fine for dwg files (dwg to dwg). 

When I want to use it on dxf (dxf to dwg) (I am switching the file extension), CAD allows me to select the source file but then error shows. 

However, if I manually open the source dxf, and run the command in the dwg the program will work. 

My guess there must be an issue with LISP not being able to open dxf.

Can you help?

(vl-load-com)
  
(command "MODEL" "")
(command "ZOOM" "E")


  (setq acobj (vlax-get-acad-object)
        acdoc (vla-get-activedocument acobj) ;;== Gets the active document/current drawing
        model (vla-get-modelspace acdoc) ;;== To tell AutoCAD which space the new entities should occupy, you need to obtain a pointer to that space.
        docs  (vla-get-documents acobj)
	targets '((0 10000) (15000 10000) (30000 10000))
  )
  ;;== Checking the AutoCAD version as some of the functions are version dependent 
  (setq dbx
    (vla-GetInterfaceObject acobj
      (if
        (< (setq acver (atoi (getvar "ACADVER"))) 16)
        "ObjectDBX.AxDbDocument"
        (strcat "ObjectDBX.AxDbDocument." (itoa acver))
      )
    )
  );;== Check end


  (setq cntr 0) ;;= Variable to count number of loops to be performed
  
  (setq reference_point (vlax-3d-point 0.0 0.0 0.0)) ;;== creates a point at given coordinates, required for most operations on objects
  (while 
    (and
      (<= cntr 0) ;;== loop counter condition === should be 2, set to 0 for testing!!!
      (progn
        (princ)
        (setq file (getfiled "Select file to import lines" (cond (path) ((getvar 'dwgprefix))) "dwg" 2)) ;;== getfiled - Prompts the user for a file name with the standard AutoCAD file dialog box, and returns that file name, 
      )
      ;;(setq insertion_point (getpoint "\nSpecify insertion point:(command "._VBASTMT" (strcat "AcadApplication.Documents.Open \" "C:\Users\320049837\Desktop\MRI\TopView.dxf" "\" "")) "))
      (setq insertion_point (nth cntr targets))  							;;== Goes through the list of target positions and sets the current target accordingly
      (setq insertion_point (vlax-3d-point insertion_point))
      ;;(command "._VBASTMT" (strcat "AcadApplication.Documents.Open \"" file "\""))
    )
    (setq path (strcat (vl-filename-directory file) "\\")
          doc nil
          l nil
    ) 
    (vlax-for x docs
      (if
        (eq (vla-get-fullname x) file)
        (setq doc x)
      )
    )
    (or doc (vla-open dbx file :vlax-true))
    (vlax-for obj (vla-get-modelspace (cond (doc) (dbx)))
      (if
        (eq (vla-get-objectname obj) "AcDbPolyline")
        (setq l (cons obj l))
      )
    )
    (prompt "n\Stop 1")
    (if l
      (foreach x
        (vlax-invoke (cond (doc) (dbx)) 'copyobjects l model)
        (vla-move x reference_point insertion_point)
      )
    )
    (princ (strcat "\n" (itoa (length l)) " lines imported from " (strcase (vl-filename-base file))))
    (MRIcolours)		;;==Adding colours for the lines - subfunction)
    (MRItexts)
    (setq cntr (1+ cntr)) 	;;==loop counter increment
  ) 
  (vlax-release-object dbx)
  (princ)
0 Likes