Jump to content

Select rectangle (polyline) and plot within those vertex


jonthan313

Recommended Posts

I am able to plot to PNG with a lisp, but during the process I have to drag a window of the rectangle of what I am trying to plot within. (Doing screenshots for work). I want to be able to type in a command --> select polyline --> plot within the vertices. Is there a way to get the 2 (two) vertex points, the upper left and lower right, save the coordinates and put them back in during the plot lisp?

 

Thank you

Link to comment
Share on other sites

Welcome to CADTutor jonthan :)

 

The following example will prompt the user to select an LWPolyline object and will return the lower-left and upper-right coordinates of the vertex extents (for a rectangular polyline, this corresponds to the lower-left & upper-right corner vertices):

([color=BLUE]defun[/color] getwindowpoints ( [color=BLUE]/[/color] ent )
   ([color=BLUE]while[/color]
       ([color=BLUE]progn[/color] ([color=BLUE]setvar[/color] 'errno 0) ([color=BLUE]setq[/color] ent ([color=BLUE]car[/color] ([color=BLUE]entsel[/color] [color=MAROON]"\nSelect Polyline: "[/color])))
           ([color=BLUE]cond[/color]
               (   ([color=BLUE]=[/color] 7 ([color=BLUE]getvar[/color] 'errno))
                   ([color=BLUE]princ[/color] [color=MAROON]"\nMissed, try again."[/color])
               )
               (   ([color=BLUE]=[/color] 'ename ([color=BLUE]type[/color] ent))
                   ([color=BLUE]if[/color] ([color=BLUE]/=[/color] [color=MAROON]"LWPOLYLINE"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 ([color=BLUE]entget[/color] ent))))
                       ([color=BLUE]princ[/color] [color=MAROON]"\nPlease select an LWPolyline."[/color])
                   )
               )
           )
       )
   )
   ([color=BLUE]if[/color] ent
       (   ([color=BLUE]lambda[/color] ( lst ) ([color=BLUE]mapcar[/color] '([color=BLUE]lambda[/color] ( x ) ([color=BLUE]apply[/color] '[color=BLUE]mapcar[/color] ([color=BLUE]cons[/color] x lst))) '([color=BLUE]min[/color] [color=BLUE]max[/color])))
           ([color=BLUE]mapcar[/color] '[color=BLUE]cdr[/color] ([color=BLUE]vl-remove-if-not[/color] '([color=BLUE]lambda[/color] ( x ) ([color=BLUE]=[/color] 10 ([color=BLUE]car[/color] x))) ([color=BLUE]entget[/color] ent)))
       )
   )
)

You could call the above function from your program and assign the points returned by the function to the variables in your program storing the plot window points.

Link to comment
Share on other sites

So it gave me the right points, but how can I assign those coordinates to variables?

 

Assign the variables the points returned by the function, e.g.:

(if (setq lst (getwindowpoints))
   (setq pt1 (car  lst)
         pt2 (cadr lst)
   )
)

Or:

(mapcar 'set '(pt1 pt2) (getwindowpoints))

Link to comment
Share on other sites

Please bare with me since I am new at this and still learning how to write Lisp programs, but I dont know where to insert the code where you assign the variables. :( Can you please help me out?

Link to comment
Share on other sites

Sorry for the long code still working on how to


properly code, it's just more of a script than lisp. I want to get rid of the two pauses and automatically pick the points from your code. I need help implementing your code into mine.

(defun C:HMI ()

(command "-plot" 

;Detailed plot configuration?
"Yes" 

;Enter Layout Name
"Model" 

;Enter an output device name
"PublishToWeb PNG.pc3" 

;Enter Paper Size (User Defined)
"HMI" 

;Enter Drawing Orientation
"L" 

;Plot Upside Down?
"No" 

;Enter Plot Area
"Window" 

;Pause the lisp for users input of points
pause 
pause 

;Enter Plot Scale
"Fit" 

;Enter plot offset
"center" 

;Plot with Style
"yes" 

;Enter plot style table name (none)
"." 

;Plot with lineweights?
"yes" 

;Enter Shade Plot Settings
"As Displayed"

;Creating plot file and saving
pause 

;Save Changes to page setup
"no" 

;Proceed with plot
"yes")
) 

Link to comment
Share on other sites

Try the following:

(defun c:hmi ( / lst )
   (if (setq lst (getwindowpoints))
       (command
           "_.-plot"
           "_Yes"  ; Detailed plot configuration?
           "Model" ; Enter Layout Name
           "PublishToWeb PNG.pc3" ; Enter an output device name
           "HMI" ; Enter Paper Size (User Defined)
           "_L"  ; Enter Drawing Orientation
           "_N"  ; Plot Upside Down?
           "_W"  ; Enter Plot Area
           "_non" (car  lst)
           "_non" (cadr lst)
           "_F"  ; Enter Plot Scale
           "_C"  ; Enter plot offset
           "_Y"  ; Plot with Style
           "."   ; Enter plot style table name (none)
           "_Y"  ; Plot with lineweights?
           "As Displayed" ; Enter Shade Plot Settings
           "\\"  ; Creating plot file and saving
           "_N"  ; Save Changes to page setup
           "_Y"  ; Proceed with plot
       )
   )
   (princ)
)

(defun getwindowpoints ( / ent )
   (while
       (progn (setvar 'errno 0) (setq ent (car (entsel "\nSelect Polyline: ")))
           (cond
               (   (= 7 (getvar 'errno))
                   (princ "\nMissed, try again.")
               )
               (   (= 'ename (type ent))
                   (if (/= "LWPOLYLINE" (cdr (assoc 0 (entget ent))))
                       (princ "\nPlease select an LWPolyline.")
                   )
               )
           )
       )
   )
   (if ent
       (   (lambda ( lst ) (mapcar '(lambda ( x ) (apply 'mapcar (cons x lst))) '(min max)))
           (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget ent)))
       )
   )
)
(princ)

Link to comment
Share on other sites

Thank you so much Lee Mac, you are the best! Maybe I am asking too much, but would it be possible to select multiple rectangles polylines and save each of them individually with different names?

Link to comment
Share on other sites

I'm not sure what naming convention you are looking to use, but try the following:

(defun c:hmi ( / i s )
   (if (setq s (ssget '((0 . "LWPOLYLINE"))))
       (repeat (setq i (sslength s))
           (apply 'plotwindow (windowpoints (ssname s (setq i (1- i)))))
       )
   )
   (princ)
)

(defun plotwindow ( p q )
   (command
       "_.-plot"
       "_Yes"  ; Detailed plot configuration?
       "Model" ; Enter Layout Name
       "PublishToWeb PNG.pc3" ; Enter an output device name
       "HMI" ; Enter Paper Size (User Defined)
       "_L"  ; Enter Drawing Orientation
       "_N"  ; Plot Upside Down?
       "_W"  ; Enter Plot Area
       "_non" p "_non" q
       "_F"  ; Enter Plot Scale
       "_C"  ; Enter plot offset
       "_Y"  ; Plot with Style
       "."   ; Enter plot style table name (none)
       "_Y"  ; Plot with lineweights?
       "As Displayed" ; Enter Shade Plot Settings
       "\\"  ; Creating plot file and saving
       "_N"  ; Save Changes to page setup
       "_Y"  ; Proceed with plot
   )
)

(defun windowpoints ( ent )
   (   (lambda ( lst ) (mapcar '(lambda ( x ) (apply 'mapcar (cons x lst))) '(min max)))
       (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget ent)))
   )
)
(princ)

Link to comment
Share on other sites

I tried your code and I selected two polylines, but when one rectangle polyline plots, a PNG is created, but then it's overwritten by the next polyline plot. It would be nice if it would be able to create two different PNG with _A, _B, _C...etc at the end or some other naming convention that differs the files.

Link to comment
Share on other sites

Any luck with the code Mr. Lee Mac?

 

Please understand that my time participating on this forum is voluntary; if you require a more prompt response, we can discuss an hourly rate.

Link to comment
Share on other sites

Try this:

([color=BLUE]defun[/color] c:hmi ( [color=BLUE]/[/color] f i s )
   ([color=BLUE]setq[/color] f ([color=BLUE]strcat[/color] ([color=BLUE]getvar[/color] 'dwgprefix) ([color=BLUE]vl-filename-base[/color] ([color=BLUE]getvar[/color] 'dwgname))))
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] s ([color=BLUE]ssget[/color] '((0 . [color=MAROON]"LWPOLYLINE"[/color]))))
       ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] i ([color=BLUE]sslength[/color] s))
           ([color=BLUE]apply[/color] 'plotwindow ([color=BLUE]cons[/color] f (windowpoints ([color=BLUE]ssname[/color] s ([color=BLUE]setq[/color] i ([color=BLUE]1-[/color] i))))))
       )
   )
   ([color=BLUE]princ[/color])
)

([color=BLUE]defun[/color] plotwindow ( f p q )
   ([color=BLUE]command[/color]
       [color=MAROON]"_.-plot"[/color]
       [color=MAROON]"_Yes"[/color]  [color=GREEN]; Detailed plot configuration?[/color]
       [color=MAROON]"Model"[/color] [color=GREEN]; Enter Layout Name[/color]
       [color=MAROON]"PublishToWeb PNG.pc3"[/color] [color=GREEN]; Enter an output device name[/color]
       [color=MAROON]"HMI"[/color] [color=GREEN]; Enter Paper Size (User Defined)[/color]
       [color=MAROON]"_L"[/color]  [color=GREEN]; Enter Drawing Orientation[/color]
       [color=MAROON]"_N"[/color]  [color=GREEN]; Plot Upside Down?[/color]
       [color=MAROON]"_W"[/color]  [color=GREEN]; Enter Plot Area[/color]
       [color=MAROON]"_non"[/color] p [color=MAROON]"_non"[/color] q
       [color=MAROON]"_F"[/color]  [color=GREEN]; Enter Plot Scale[/color]
       [color=MAROON]"_C"[/color]  [color=GREEN]; Enter plot offset[/color]
       [color=MAROON]"_Y"[/color]  [color=GREEN]; Plot with Style[/color]
       [color=MAROON]"."[/color]   [color=GREEN]; Enter plot style table name (none)[/color]
       [color=MAROON]"_Y"[/color]  [color=GREEN]; Plot with lineweights?[/color]
       [color=MAROON]"As Displayed"[/color] [color=GREEN]; Enter Shade Plot Settings[/color]
       (uniquefilename f [color=MAROON]".png"[/color])
       [color=MAROON]"_N"[/color]  [color=GREEN]; Save Changes to page setup[/color]
       [color=MAROON]"_Y"[/color]  [color=GREEN]; Proceed with plot[/color]
   )
)

([color=BLUE]defun[/color] uniquefilename ( pth ext [color=BLUE]/[/color] fnm tmp )
   ([color=BLUE]if[/color] ([color=BLUE]findfile[/color] ([color=BLUE]setq[/color] fnm ([color=BLUE]strcat[/color] pth ext)))
       ([color=BLUE]progn[/color]
           ([color=BLUE]setq[/color] tmp 1)
           ([color=BLUE]while[/color] ([color=BLUE]findfile[/color] ([color=BLUE]setq[/color] fnm ([color=BLUE]strcat[/color] pth [color=MAROON]"("[/color] ([color=BLUE]itoa[/color] ([color=BLUE]setq[/color] tmp ([color=BLUE]1+[/color] tmp))) [color=MAROON]")"[/color] ext))))
       )
   )
   fnm
)

([color=BLUE]defun[/color] windowpoints ( ent )
   (   ([color=BLUE]lambda[/color] ( lst ) ([color=BLUE]mapcar[/color] '([color=BLUE]lambda[/color] ( x ) ([color=BLUE]apply[/color] '[color=BLUE]mapcar[/color] ([color=BLUE]cons[/color] x lst))) '([color=BLUE]min[/color] [color=BLUE]max[/color])))
       ([color=BLUE]mapcar[/color] '[color=BLUE]cdr[/color] ([color=BLUE]vl-remove-if-not[/color] '([color=BLUE]lambda[/color] ( x ) ([color=BLUE]=[/color] 10 ([color=BLUE]car[/color] x))) ([color=BLUE]entget[/color] ent)))
   )
)
([color=BLUE]princ[/color])

Link to comment
Share on other sites

I really appreciate all of the help Mr. Lee Mac! :) I know you take time to do this for a lot of people and expect nothing in return, but if theres anything you need please let me know. Thank you so much!!!

Link to comment
Share on other sites

  • 1 year later...

Hello

I appreciate of sharing knowledge

I tried to change your code to pick rectangle and plot rectangle and anything in rectangle with fit to paper

what can i do??

Edited by hosyn
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...