Swap between opened dwg files

Swap between opened dwg files

franciscovHWABV
Contributor Contributor
1,372 Views
9 Replies
Message 1 of 10

Swap between opened dwg files

franciscovHWABV
Contributor
Contributor

Hi everyone.

 

I would like to know if there is any way from autolisp code of changing between two already opened dwg files. The thing is that I need to take some information and modify the first drawing and after that use the information from this to modify some other things on the second drawing.

 

So as there is a way to change from one sheet to another inside the very same drawing I was asking if I can do the same thing between two different opened drawings.

 

Thanks!

0 Likes
1,373 Views
9 Replies
Replies (9)
Message 2 of 10

Kent1Cooper
Consultant
Consultant

Unfortunately, an AutoLisp routine cannot start in one drawing and continue in another.  You would probably need to use one routine to collect the information in the first drawing, and copy it to the Clipboard or the Blackboard space or to Environment Variables or something, and then use another routine to make use of that information in the second drawing.

Kent Cooper, AIA
0 Likes
Message 3 of 10

hak_vz
Advisor
Advisor

What kind of information you need to share between opened drawings?

Miljenko Hatlak

EESignature

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.
0 Likes
Message 4 of 10

ronjonp
Advisor
Advisor

Look into Design Center (DC)

ronjonp_0-1646338131039.png

 

0 Likes
Message 5 of 10

Sea-Haven
Mentor
Mentor

You can do some things with open dwgs 

 

(defun openblk (blkname / adocs)
(setq acDocs (vla-get-documents (vlax-get-acad-object)))
(vla-open acDocs blkname)
(vla-activate (vla-item acdocs 1))
;(vla-put-activedocument (vlax-get-acad-object) (vla-item acdocs 1))
)

we used something like this to "steal objects from 1 dwg to another like Kent using copy and paste. 

0 Likes
Message 6 of 10

franciscovHWABV
Contributor
Contributor

Hi all and thanks for your answers!

Basically I will have two opened files: the first will be for 3D objects where I will get some distances and store them into variables in order to draw some views and sections on the second file, which will be only for 2D elements.

I have been trying with the below code, playing with opening and closing files in order to move between active drawings, but it does not work.

 

This code is a simple example of getting coordinates of two selected points in first drawing in order to draw a line between them on the second one.

 

(defun c:3dprogram ()
 
  ;SAVE 3D FILE PATH
 
  (setq 3Dfilepath (getvar "dwgprefix"))
  (setq 3Dfilename (getvar "dwgname"))
 
  (setq 3Dfilepath (strcat 3Dfilepath 3Dfilename))
 
  (setq leftpoint (getpoint "\nLeft point : "))
  (setq rightpoint (getpoint "\nRight point : "))
   
  ;OPEN 2D FILE
 
  (setq 2dfile (findfile (getfiled "Select file" "" "dwg" 8)))
 
  (setq 2Dfilepath (vl-filename-directory 2dfile))
  (setq 2Dfilename (vl-filename-base 2dfile))
 
  (setq 2Dfilepath (strcat 2Dfilepath "\\" 2Dfilename ".dwg"))
 
 
  (vla-open
    (vla-get-documents
      (vla-get-application (vlax-get-acad-object))
    )
    2Dfilepath
  )    
 
  ;CLOSE 3D FILE
 
  (command "_qsave")
  (command "_close")
 
  (command "_pline" leftpoint rightpoint "")

  (princ)
)
 
The error comes like this: error: "failed to set debug break position"
I presume this is because the code is loaded only in the first 3D file so when the 3D file closes it is not capable to draw the pline on the second file. Any thoughts? I thought that, the same way there are programms working for a batch of drawings this would be possible between two of them.
0 Likes
Message 7 of 10

hak_vz
Advisor
Advisor

Here is a sample code used to get distance (function gd) , get point (function gp) , or simply set some value (function sv), and make it available through all open drawings.

 

(defun twodec (x)(atof(rtos x 2 3)))

(defun c:gd (/ pp _name)
;get distance and assign to variable name
	(setq pp (atof(rtos(distance (getpoint "\nSelect first point > ")(getpoint "\nSelect second point > ")) 2 5)))
	(setq _name (getstring "\n Variable name for distance > "))
    (set (read _name) pp)
	(vl-propagate (read _name))
	(strcat _name " = " (rtos pp 2 5))
)

(defun c:gp ( / pp _name)
;get point and assign to variable name
	(setq pp (mapcar 'twodec (getpoint "\nSelect point > "))
		_name (getstring "\n Variable name > "))
    
	(set (read _name) pp)
	(vl-propagate (read _name))
	pp
)

(defun c:sv( / pp _name)
	;assign value to variable name
	(setq pp (getstring "\n Enter value or expression >"))
	(setq _name (getstring "\n Variable name> "))	
    (set (read _name) (eval (read pp)))
	(vl-propagate (read _name))

)

 

Regarding to your description, to take a point you have to run command gp and assign points to left point and right point (3d points) in your drawing with 3d entity.  This variables will be set in all open drawings and also in your drawing where you have to create 2d line. You should create a code that works in 2d and creates 2d lines from 3d points from global variables leftpoint and rightpoint.

 

 

 

Miljenko Hatlak

EESignature

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.
0 Likes
Message 8 of 10

franciscovHWABV
Contributor
Contributor

Thanks very much @hak_vz !

I have used your vl-propagate to get the variables into all activated drawings and also the below line to switch easily between open files:

 

(vla-activate (vla-item (vla-get-documents (vlax-get-acad-object)) (strcat 2Dfilename ".dwg")))
 
After this line, with the 2d file active I would need to keep going creating more stuff on it, the problem is that the code stops when changing the active drawing. Is there a way to do that? Switch the active drawing and keep running the code on it.

 

 

0 Likes
Message 9 of 10

hak_vz
Advisor
Advisor

@franciscovHWABV 

 

Each open drawing works in its own closed environment (namespace) so switching and running between drawings doesn't work.

 General procedure would be to run functions in first drawing and collect variables (functions are finished, variables are propagated), Switch to another drawing and run other functions that utilize previously changed variables (same global variables in all drawings). Drawings are kept open all the time and there is no need for closing them.

 

Another option is by using output file to store variables. But again, there is no way to directly  switch between drawings.

Miljenko Hatlak

EESignature

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.
0 Likes
Message 10 of 10

franciscovHWABV
Contributor
Contributor

Ok @hak_vz , thanks!

After one year learning and doing anything I could imagine with lisp it is hard to admit something that simple is impossible. I will have to go with one of the alternatives you have mentioned!

0 Likes