+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 30
  1. #1
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    31

    Question Learning Lisp "Polyline Width"

    Registered forum members do not see this ad.

    I'm still learning Lisps and am in the need of a function. I'm not going to say what i need it for, i want to try and write the lisp from the input of this forum.

    I need to know how to store the polyline width of a user selected polyline. for example, if i select a polyline and i run my lisp, or vice versa, the lisp will store the global width of the selected polyline into memory.

    (setq PW (ssget "polyline width")

    After i get this info, i will post my lisp and what it does. After you guys can share how you would have done it instead. and it will probably be better then my way.

    thanks ahead of time.

  2. #2
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,364

    Default

    Assuming you are referring to the global (constant) width of an LWPolyline, the width value is associated with DXF group 43:
    Code:
    (defun c:test ( / ent enx )
        (if (setq ent (car (entsel)))
            (if (= "LWPOLYLINE" (cdr (assoc 0 (setq enx (entget ent)))))
                (princ (strcat "\nThe polyline width is " (rtos (cdr (assoc 43 enx)))))
                (princ "\nThe selected object is not a polyline.")
            )
            (princ "\nNo object selected.")
        )
        (princ)
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  3. #3
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    31

    Default

    Quote Originally Posted by Lee Mac View Post
    (defun c:test ( / ent enx )
    (if (setq ent (car (entsel)))
    (if (= "LWPOLYLINE" (cdr (assoc 0 (setq enx (entget ent)))))
    (princ (strcat "\nThe polyline width is " (rtos (cdr (assoc 43 enx)))))
    (princ "\nThe selected object is not a polyline.")
    )
    (princ "\nNo object selected.")
    )
    (princ)
    )
    this is kind of what i'm looking for, and maybe i can take it from here.
    instead of String output, i want to put the value into memory to use within another command.
    let me take a shot at it, and if i can't figure it out, i'll reach out for more help. Thanks!

  4. #4
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    31

    Default

    Quote Originally Posted by Lee Mac View Post
    Assuming you are referring to the global (constant) width of an LWPolyline, the width value is associated with DXF group 43:[/CODE]
    Thanks to YOU! I did it!!! probably not as good as you would have done it, but i did it!

    Code:
    (defun c:px nil
        (if (setq ent (car (entsel)))
            (if (= "LWPOLYLINE" (cdr (assoc 0 (setq enx (entget ent)))))
                (setq pw (rtos (cdr (assoc 43 enx)))))
        )
    (command "explode" ent)
    (command "pedit" "m" "p" "" "w" pw"")
        (princ)
    )
    This lisp xplodes the polyline while keeping the width.
    Technically, it gets the width, stores it in memory, explodes the LWPOLYLINE, converts the lines to LWPOLYLINE then adds the width of "pw"

    it just clicked that i may need to add a "peditaccept" just incase most people have it set to 0.

    I am curious as to how the better approach would be.

  5. #5
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,888

    Default

    Rather than calling it save in memory which is correct, you are saving a "Global" variable for further use in the same dwg session. The only problem may be if you write another program that uses pw. You can save variables within the dwg such as USERI1-5 5 integers, USERR1-5 5 reals
    But again they can be overridden. If saving global variables maybe use a prefix GLBpw and keep a list somewhere of what you have used with a description.

    Code:
    (setvar "userr1" 0.25) ; sets userr1 value
     
    (getvar "userr1") ; retrieves the value
    Code:
    (defun c:px ( / ent) ; implies ent is a local variable and will not be saved at end of defun
     
    just me added a space after pw 
    (command "pedit" "m" "p" "" "w" pw "")
    
    Almost forgot if your wanting to save a lot of variables and use them across drawings then save to a simple txt file and just read as required. Make it a library defun in acaddoc.lsp then can use when ever.
    A man who never made mistakes never made anything

  6. #6
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    31

    Default

    Quote Originally Posted by BIGAL View Post
    Rather than calling it save in memory which is correct, you are saving a "Global" variable for further use in the same dwg session.
    Thanks.
    I'm still learning Lisp and kind of understand the memory within the lisp. i used nil, because i wasn't 100% confident. i do understand the importance of it, so i'll add it just in case other lisps end up using "pw" as a variable.

    Thanks everyone.

  7. #7
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,364

    Default

    Quote Originally Posted by RubberDinero View Post
    Thanks to YOU! I did it!!! probably not as good as you would have done it, but i did it!
    Well done!

    Quote Originally Posted by RubberDinero View Post
    I am curious as to how the better approach would be.
    There are numerous ways in which the code could be developed further in order to account for and mitigate against every possibility of an error - an initial improvement would be to account for a null or invalid selection, e.g.:
    Code:
    (defun c:px ( / cmd ent enx ped )
        (if (setq ent (car (entsel)))
            (if (= "LWPOLYLINE" (cdr (assoc 0 (setq enx (entget ent)))))
                (progn
                    (setq cmd (getvar 'cmdecho)
                          ped (getvar 'peditaccept)
                    )
                    (setvar 'cmdecho 0)
                    (setvar 'peditaccept 1)
                    (command
                        "_.explode" ent
                        "_.pedit" "_m" "_p" "" "_w" (cdr (assoc 43 enx)) ""
                    )
                    (setvar 'peditaccept ped)
                    (setvar 'cmdecho cmd)
                )
                (princ "\nThe selected object is not an LWPolyline.")
            )
            (princ "\nNo object selected.")
        )
        (princ)
    )
    But one could go further to account for locked layers, with checks to ensure that valid objects were generated following the call to the EXPLODE command prior to invoking the PEDIT command.
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  8. #8
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,158

    Default

    Quote Originally Posted by Lee Mac View Post
    But one could go further to account for locked layers, with checks to ensure that valid objects were generated following the call to the EXPLODE command prior to invoking the PEDIT command.
    Hi Lee,
    I was curious would it be possible to account for locked layer with the entsel/nentsel approach, without using the:
    Code:
    (ssget "_+.:E:S:L")
    I thought that the only way would be acessing entity's layer definition and retrieve from there if its status is locked. So the above line shows how much this issue is simplified. Another way I could think of is something like:
    Code:
    (setq s (ssadd))
    (ssadd (car (entsel "\nSelect entity: ")) s)
    (sssetfirst nil s)
    (/= 0 (sslength (ssget "_.+:I:L"))); here we can check if the selected entity is on locked layer
    (sssetfirst nil nil)

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,364

    Default

    Here is an example:
    Code:
    (defun c:px ( / cmd ent enx ped )
        (setvar 'errno 0)
        (while (/= 52 (getvar 'errno))
            (setvar 'errno 0)
            (setq ent (car (entsel "\nSelect polyline <exit>: ")))
            (cond
                (   (= 7 (getvar 'errno))
                    (prompt "\nMissed, try again.")
                )
                (   (null ent)
                    (prompt "\nExit.")
                )
                (   (/= "LWPOLYLINE" (cdr (assoc 0 (setq enx (entget ent)))))
                    (prompt "\nSelected object is not an LWPolyline.")
                )
                (   (= 4 (logand 4 (cdr (assoc 70 (tblsearch "layer" (cdr (assoc 8 enx)))))))
                    (prompt "\nSelected polyline is on a locked layer.")
                )
                (   (setq cmd (getvar 'cmdecho)
                          ped (getvar 'peditaccept)
                    )
                    (setvar 'cmdecho 0)
                    (setvar 'peditaccept 1)
                    (command
                        "_.explode" ent
                        "_.pedit" "_m" "_p" "" "_w" (cdr (assoc 43 enx)) ""
                    )
                    (setvar 'peditaccept ped)
                    (setvar 'cmdecho cmd)
                )
            )
        )
        (princ)
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  10. #10
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,158

    Default

    Registered forum members do not see this ad.

    Thank you, Lee
    I've just wrote something similar:
    Code:
    (defun C:test ( / e enx )
    	(while 
    		(not
    			(and
    				(setq e (car (entsel "\nSelect LWpolyline on non-locked layer: ")))
    				(eq (cdr (assoc 0 (setq enx (entget e)))) "LWPOLYLINE")
    				(/= (cdr (assoc 70 (entget (tblobjname "LAYER" (cdr (assoc 8 enx)))))) 4)
    			)
    		)
    		e
    	)
    	
    	(print enx); if successful
    	(princ)
    )
    I might need to study deeper this logand function as its not the first time I see its in use.
    I think I'm done writing codes for today.

Similar Threads

  1. [LISP] Scaling just the "x" and "y" axis
    By chiimayred in forum AutoLISP, Visual LISP & DCL
    Replies: 32
    Last Post: Yesterday, 04:49 pm
  2. HELP to include "AcDbAlignedDimension" code for a "Dimension deleting" LISP
    By vernonlee in forum AutoLISP, Visual LISP & DCL
    Replies: 14
    Last Post: 10th Apr 2015, 04:03 pm
  3. Automatically updating "defined width" attributes for MTEXT?
    By Comatosis in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 1
    Last Post: 8th Dec 2011, 09:59 pm
  4. Polyline global width keeps showing up as 1"
    By ammobake in forum AutoCAD General
    Replies: 5
    Last Post: 4th Jun 2009, 10:52 pm
  5. Finding the "width" of a block (.NET)
    By askrius in forum AutoLISP, Visual LISP & DCL
    Replies: 0
    Last Post: 8th Sep 2008, 10:21 pm

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts