Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Copy Polylines from One Drawing onto the Same Sport of a Sister Drawing?

33 REPLIES 33
SOLVED
Reply
Message 1 of 34
JC_BL
2627 Views, 33 Replies

How to Copy Polylines from One Drawing onto the Same Sport of a Sister Drawing?

I would like to know the way to correctly copy and paste selected polylines from one drawing onto the same spot of a sister drawing.

 

I am trying to do the following using AutoLISP:

  • My program opens a master-drawing, and create a simple layout in that drawing.  The layout covers everything on the drawing.
  • The program uses "ExportLayout" command to export the layout into a DWG file.  Let call this DWG file the "compressed drawing".  The purpose of doing this is to turn all the 3D objects into 2D and to significantly reduce the file size; then users can open that drawing in AutoCAD-360 without running the risk of hitting the maximum drawing file size limit of AutoCAD-360.
  • The program saves the compressed-drawing in AutoCAD-360 (it used to be called AutoCAD WS).
  • A user opens that compressed-drawing in AutoCAD-360, and he puts some polylines over some objects on the drawing to indicate that he has finished working on those objects.
  • My program gets the compressed drawing back from AutoCAD-360.
  • The program copies and pastes the polylines from the compressed-drawing into the master-drawing.  It uses "Copy with Base Points" and "Paste to Original Coordinates" to copy and paste.
  • The program uses the polylines to select the objects in the master-drawing.  It finds the entity handles of those selected objects and use the entity handles as the key to update some database tables.

Everything works _except_ the copying and pasting of the polylines.  I find that the polylines copied to the master drawing become very tiny and they are placed in the wrong place.  Seem like the origins / scales of the master drawing and the compressed drawing are different.

 

How do I find which origin and which scale the master drawing uses?

How do I change the compressed drawing to match the origin and the scale of the master drawing?

 

 

Please help me with these.  Thanks in advance for your help.

 

Jay Chan

33 REPLIES 33
Message 2 of 34
JC_BL
in reply to: JC_BL

OK, I have found the way to deal with the scale.

I find the scale of the current layout of the master drawing using this command:

(- (car (trans '(1 0 0) 2 3))
(car (trans '(0 0 0) 2 3))
)

Convert it from a scale-down ratio into a scale-up ratio. Then use the scale-up ratio as the parameter to the INSERT command that I use to paste the block of polylines on the master drawing.

I didn't realize that I had written down those commands above in a file months ago.

Now, I just need to know how to deal with the "origin". Please help. Thanks.

Jay Chan
Message 3 of 34
Lee_Mac
in reply to: JC_BL

Message 4 of 34
GTVic
in reply to: JC_BL

Would this solve the problem?

 

(command "ucs" "World")
' copy stuff
(command "ucs" "Prev")

(command "ucs" "World")
' paste stuff
(command "ucs" "Prev")

 

Message 5 of 34
JC_BL
in reply to: GTVic

No, that doesn't work. May be I don't do what you have suggested in the correct context. What I did is to use your method to copy the polylines from the DWG file that was generated using EXPORTLAYOUT command, and then use your method to paste the polylines onto the master-drawing. This doesn't work -- meaning that the polylines are pasted to the wrong spot.

Seem like the problem has to do with the offset of the Layout (and the resulted DWG file) is different from the offset of the master-drawing. And I need to figure out what the offset of the Layout (and the resulted DWG file) and then compensate for the offset when I paste the polylines back to the master-drawing.

I am still looking...

Jay Chan
Message 6 of 34
dbroad
in reply to: JC_BL

I don't really know how you would do what you intend and am not sure a programmatic solution would be a feasible use of your time.  Exportlayout creates a modelspace that contains the contents of both the model viewports and the paper space.  So it creates a mixed unit, mixed scale mish-mash with no-real backward links to the original.  You would have to develop a method of recognizing each viewport, it's scale and view center.

 

One possible solution would be to create a second exportlayout drawing with legend markers interpreting the various scales to allow you to rebuild the backward links.

 

If that is not the approach you want, you would need to use trans for each viewport in the modelspace (viewport ename to paperspace ename). Pasteorig should be to paperspace in the master drawing.  From there, the program would have to select objects within a window that encompassed paper space and other viewport spaces.

Architect, Registered NC, VA, SC, & GA.
Message 7 of 34
JC_BL
in reply to: dbroad

I am not quite sure I understand this. What did you mean when you said "(viewport ename to paperspace ename)"?

Let me re-phrase my question:
Let say I create a new layout in a drawing. By default, the new layout is scaled-to-fit everything in a paper space. That is great. I see that the upper left corner of the new layout is (0'-0", 0'-0", 0'-0"). I want to know the XY-coordinate in the model space that is corresponding to that (0'-0", 0'-0", 0'-0") in the new layout.

I have been looking for answer of this question using Google and somehow I cannot any.

Please help. Thanks.

Jay Chan
Message 8 of 34
GTVic
in reply to: JC_BL


@jchan wrote:
No, that doesn't work. May be I don't do what you have suggested in the correct context. What I did is to use your method to copy the polylines from the DWG file that was generated using EXPORTLAYOUT command, and then use your method to paste the polylines onto the master-drawing. This doesn't work -- meaning that the polylines are pasted to the wrong spot.

Seem like the problem has to do with the offset of the Layout (and the resulted DWG file) is different from the offset of the master-drawing. And I need to figure out what the offset of the Layout (and the resulted DWG file) and then compensate for the offset when I paste the polylines back to the master-drawing.

I am still looking...

Jay Chan

Sorry, my fault for not reading carefully about the layout export.

 

Copy with base point is good. Paste to Original Coordinates is an issue. I think the regular paste would be best, then you can pick a common point. That will not solve the rotation scale issue but it will solve the location problem.

 

I'm trying to think of a low-tech solution.

 

  1. Create a base point object in the main drawing. For example a line of 1 unit length
  2. When copying from the WS drawing, use the Copy with base point and pick the end of that line. Include the line in the copy.
  3. When pasting, use the regular paste and select the same end point as the paste location.
  4. A simple LISP program can select all of the pasted entities. See below.
  5. Rotate and scale so the two lines match.

 

(defun c:paste_selectp ( / lastent paste_ss )
  (setq lastent (entlast) paste_ss (ssadd))
  (command "PASTECLIP")
  (while (= (getvar "CMDNAMES") "PASTECLIP")
    (command pause))
  (while (setq lastent (entnext lastent))
    (ssadd lastent paste_ss))

  (command "SELECT" paste_ss "")
  
  (princ "\nDone - Pasted Entities are Now Previous Selection")
  (princ))

 

Message 9 of 34
GTVic
in reply to: GTVic

This function will rotate, move and scale entities based on 4 reference points:

 

(defun c:transform ( / ss angbase angdir p1 p2 p3 p4 a1 a2 d1 d2 )
  (setq ss (ssget))
  (setq p1 (getpoint "\nPoint 1 on New Entity: ")
p2 (getpoint "\nPoint 2 on New Entity: "))
  (setq p3 (getpoint "\nPoint 1 on Reference Entity: ")
p4 (getpoint "\nPoint 2 on Reference Entity: "))
  (setq a1 (angle p1 p2)
  a2 (angle p3 p4))
  (setq d1 (distance p1 p2)
  d2 (distance p3 p4))
  (setq angbase (getvar "angbase")
  angdir (getvar "angdir"))
  (command "undo" "begin")
  (setvar "angbase" 0)
  (setvar "angdir" 0)
  (if (/= a1 a2) (command "rotate" ss "" p1 (angtos (- a2 a1) 0 8)))
  (if (/= d1 d2) (command "scale" ss "" p1 (/ d2 d1)))
  (command "move" ss "" p1 p3)
  (setvar "angbase" angbase)
  (setvar "angdir" angdir)
  (command "undo" "end")
  (princ))

 The end result is that p1 and p2 will match p3 and p4

Message 10 of 34
JC_BL
in reply to: GTVic

I know you are trying to help, and I appreciate that. Unfortunately, the solution doesn't work for me because that solution depends on someone who knows exactly where the spot is and paste the entities to the right spot. But what I am trying to do is to have an automated process to do this copying and pasting for many drawings. Therefore, there will not be anyone around to manually copy and paste.

In order to do what I want to do, I need to know the offset of an object on the Paper-Space layout relatively to the same object in the Model-Space. Or to put this in a different way: I need to know the actual coordinates in the Model-Space that is corresponding to the upper left corner of a Paper-Space layout. If I know this, I will be able to do the rest.

Any idea? Please help. Thanks.

Jay Chan
Message 11 of 34
GTVic
in reply to: JC_BL

Again, a low tech solution would be to put a predefined , possibly hidden, object in the drawing. This can be made unique with layer/color/x-data/etc and can be easily selected by the LISP application to determine the insertion/end points. As long as the object is in both drawings the process could be completely automatic.
Message 12 of 34
JC_BL
in reply to: GTVic

This sounds promising. May be I can add a text string with a unique prefix like "$$CenterPtMSCoord$$" and add it in the middle of the Model Space at a location that my AutoLISP program can determine.

May be I can embed the XY-corrdinates directly into that text string like "$$CenterPtMSCoord$$(550.00,789.00)".

That text string will become a part of the small DWG file that EXPORTLAYOUT generates. Then I can compare the coordinates of that text string in the small DWG file with the coordinates of the same text string in the master drawing, and I will find the XY-offsets.

This may work. I will try and I will let you know how this goes. Thanks for your suggestion.

Jay Chan
Message 13 of 34
hmsilva
in reply to: JC_BL

Hi jay,

like you said previously, your layout is a single viewport, scaled to fit all model, so before the Export Layout to Model, you can use the viewport object or one block or other pre-defined object, store two model space coordinated points with xdata, text, attributes or another method, and use the trans function go to translate the viewport coordinates to the model coordinates, then open the exported dwg, read the stored coordinates, move and scale the new dwg...

As a demo,

(setq VlaObj (vlax-ename->vla-object (car (entsel "\nSelect a Viewport : "))))
(vlax-invoke-method VlaObj 'GetBoundingBox 'll 'ur)
(setq psllpt (vlax-safearray->list ll))
(setq psurpt (vlax-safearray->list ur))
(setq msllpt (trans psllpt 3 2));; the viewport lower-left point - model space coordinates
(setq msurpt (trans psurpt 3 2));; the viewpor upper right point - model space coordinates

 

HTH

Henrique

EESignature

Message 14 of 34
JC_BL
in reply to: GTVic

Thanks.  I finally come very close to find a way to copy the polylines from a DWG (generated from EXPORTLAYOUT) to the correct spot in the original drawing.

 

The way that I use is very similar to what you have suggested:

 

1.  Add a small 1" circle right at the UCS of the Model Space of the master drawing.  Make sure the circle is placed on a special layer that I am sure no drawings will have.  It is the reference point.

 

2.  Create the paper-space layout as usual.

 

3.  Use the following command to find the scale-down factor of the paper-space layout:

(setq nScaleDownFactor
   (- (car (trans '(1 0 0) 2 3))
      (car (trans '(0 0 0) 2 3))
   )
)

 

4.  Convert the scale-down factor into a scale-up factor (scale-up = 1 / scale-down).  Save it into a text file.  I could have saved it within the drawing file as an object.  Saving in a text file is a tried and true method and I stick with this.

 

5.  Use EXPORTLAYOUT command to generate a small DWG file from the paper-space layout as usual.

 

6.  Open that small DWG file (that was from EXPORTLAYOUT), explode everything in order to reach the reference point.  Find the reference point based on the fact that the reference point is a circle and it is on the special layer.

 

7.  Get the coordinates of the reference point and convert into a list.  The list is like (2.568 5.789 0), and that is in term of the location of the paper-space.

 

8.  Use the SCALE command, and feed it with the coordinate of the reference point and the scale-up factor.  Now, the small DWG file should have the same scale and the same UCS of the master drawing.  Save the small DWG.

 

From here on, I can simply use "Copy with Base Point" command and the "Paste to Original Coordinates" command to copy and paste the polylines from the small DWG file to the master drawing.

 

No, I still have some work to do.  The reason is that the polylines that the program pastes onto the original drawing are still "slightly" off. Their scales are correct; but their locations are just a little bit off (like +2"-X and +7"-Y).  May be there are some decimal places mistakenly rounded off or something.  Nevertheless, I can see that I am on the right track.

 

Thanks for all the help that I have received from people in this forum.  I appreciate that.

 

Jay Chan

Message 15 of 34
JC_BL
in reply to: JC_BL

One addition info about what I said last time. The SCALE command only changes the scale of the small DWG file to match the scale of the original master drawing. It doesn't change the UCS of the small DWG file. This explains the reason why the polylines were placed a bit off when I copied and pasted the polylines from the scaled up small DWG onto the original master drawing. I need to correct the scale and then the UCS in order to avoid this problem.

Jay Chan
Message 16 of 34
GTVic
in reply to: JC_BL

Seems like a lot of UCSes to keep track of. Each layout would have a UCS for orientating the portion of DWG to best fit on a landscape sheet.

Wouldn't it be best to match the WCS/origin rather than the UCS? Then in the paste all you have to do is worry about the rotation?
Message 17 of 34
hmsilva
in reply to: JC_BL

Jay,

just trying to understand your workflow.

 

Is the original master drawing in WCS or UCS?

Is the Viewport just scaled to fit the entire master drawing, without rotating the UCS?

 

Henrique

EESignature

Message 18 of 34
JC_BL
in reply to: GTVic

Thanks for pointing this out. I have just discussed this with our draftsmen. And yes I should be using WCS instead of UCS.

Jay Chan
Message 19 of 34
JC_BL
in reply to: hmsilva

The drawing is in WCS. Although the WCS and UCS are the same in most of our drawings, for the sake of "just in case", I should stick with WCS. I will use the default layout settings that is sale-to-fit the entire master drawing.

Jay Chan
Message 20 of 34
hmsilva
in reply to: JC_BL


@jchan wrote:
The drawing is in WCS. Although the WCS and UCS are the same in most of our drawings, for the sake of "just in case", I should stick with WCS. I will use the default layout settings that is sale-to-fit the entire master drawing.

Jay Chan

As a "demo"

In the master drawing in the layout

(defun c:demo ( / LL MSLLPT MSURPT PSLLPT PSURPT UR VLAOBJ VP)
  (if (setq vp (ssget "x" '((0 . "viewport") (-4 . "/=") (69 . 1))))
    (progn
      (setq VlaObj (vlax-ename->vla-object (ssname vp 0)))
      (vlax-invoke-method VlaObj 'GetBoundingBox 'll 'ur)
      (setq psllpt (vlax-safearray->list ll)
	    psurpt (vlax-safearray->list ur)
	    msllpt (trans psllpt 3 2)
	    msurpt (trans psurpt 3 2)
      )
      (vl-propagate 'msllpt)
      (vl-propagate 'msurpt)
      (entmake
	(list
	  (cons 0 "TEXT")
	  (cons 100 "AcDbText")
	  (cons 8 "MyAlign")
	  (cons 10 psllpt)
	  (cons 40 (getvar 'TEXTSIZE))
	  (cons 1 "llpt")
	  (cons 100 "AcDbText")
	)
      )
      (entmake
	(list
	  (cons 0 "TEXT")
	  (cons 100 "AcDbEntity")
	  (cons 8 "MyAlign")
	  (cons 10 psurpt)
	  (cons 40 (getvar 'TEXTSIZE))
	  (cons 1 "urpt")
	  (cons 100 "AcDbText")
	)
      )
    )
  )
  (princ)
)

Do the export layout to model command, erase the recently created texts and  layer,

Open the new dwg, set osmode to 0 and run the demo1

(defun c:demo1 (/ LL MSLLPT MSURPT UR)
  (if (and (setq ll (ssget "x" '((0 . "Text") (8 . "MyAlign") (1 . "llpt"))))
	   (setq ur (ssget "x" '((0 . "Text") (8 . "MyAlign") (1 . "urpt"))))
	   msllpt
	   msurpt
      )
    (progn
      (setq ll (cdr (assoc 10 (entget (ssname ll 0))))
	    ur (cdr (assoc 10 (entget (ssname ur 0))))
      )
      (command "align" "_all" "" ll msllpt ur msurpt "" "Y")
    )
  )
  (princ)
)

 erase the texts and  layer...

 

The new dwg should be scaled and at the original coodinates.

 

HTH

Henrique

 

 

EESignature

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

Post to forums  

Autodesk Design & Make Report

”Boost