if then for a set of polylines

Discussion in 'AutoCAD' started by mnash, Feb 21, 2005.

  1. mnash

    mnash Guest

    Hey folks
    I have a working lisp that joins polylines within a fuzz limit, but it doesn't work when one or all of the entities are NOT polylines, because it kicks out of the lisp to ask if I want to convert entities to polylines.

    I'd like to say that if all of the entities selected are polyline then do this. Else run a bunch of extra commands to handle the difference in entities.

    SO
    if all "0" are "lwpolyline" then
    (setq SS (ssget))
    (command "pedit" "M" SS "" "J" "1" "")
    else
    convert all lines and arcs to polylines and then do
    (command "pedit" "M" SS "" "J" "1" "")
    any thoughts
    MN
     
    mnash, Feb 21, 2005
    #1
  2. mnash

    BillZ Guest

    You only will have to convert the one entity that's first chosen for the pedit command.

    This is how I do it:

    (if (= (cdr (assoc 0 (entget (ssname ss 0)))) "LWPOLYLINE")
    (command "_.pedit" (ssname ss 0) "J" ss "" "cl" "x") ;create pline offset
    (command "_.pedit" (ssname ss 0) "Y" "J" ss "" "cl" "x") ;create pline offset
    )

    Bill
     
    BillZ, Feb 21, 2005
    #2
  3. mnash

    mnash Guest

    here's what I have right now may be this can clear it up for you

    (setq SS (ssget))

    (cond
    ((= ss lwpolyline)((command "pedit" "M" SS "" "J" "1" "")(command "chprop" ss "" "c" "1" "" "")))
    (t((command "pedit" "m" ss "" "y" "j" "1" "")(command "chprop" ss "" "c" "1" ""))))

    I got the "else" portion to work, but if they are both polylines, it doesn't work for me, I'm guess I have to rewrite the first line somehow.
    MN
     
    mnash, Feb 21, 2005
    #3
  4. mnash

    Jeff Mishler Guest

    Here's one I have used in the past. I don't recall whether I wrote it or if
    I got it from this newsgroup.....I will say that it looks like the style I
    used 2 years ago......
    Change the fuzz factor to suit.

    (defun c:pedit2 (/ name idx ss ent nopoly)
    (setq idx -1)
    (prompt (strcat "\Select Objects to Join Together....."))
    (setq ss (ssget))
    (while (< (setq idx (1+ idx)) (sslength ss))
    (setq ent (entget (ssname ss idx)))
    (if (not (or (= "LWPOLYLINE" (cdr (assoc 0 ent)))
    (= "POLYLINE" (cdr (assoc 0 ent)))
    )
    )
    (setq nopoly t)
    (setq nopoly nil)
    )
    )
    (if nopoly
    (command "pedit" "m" ss "" "y" "j" "0" "")
    (command "pedit" "m" ss "" "j" "0" "")
    )
    (princ)
    )
     
    Jeff Mishler, Feb 21, 2005
    #4
  5. mnash

    Tom Smith Guest

    There's some redundancy in there. You never need to set a variable like
    nopoly nil on its first use. In this case you don't even need the nopoly
    variable at all, because it only determines which branch of the if to
    follow. You can just go straight to the if without the variable.

    (defun c:pedit2 (/ name idx ss ent etype)
    (setq idx -1)
    (prompt (strcat "\Select Objects to Join Together....."))
    (setq ss (ssget))
    (while (< (setq idx (1+ idx)) (sslength ss))
    (setq
    ent (entget (ssname ss idx))
    etype (cdr (assoc 0 ent)))
    (if (or (= "LWPOLYLINE" etype) (= "POLYLINE" etype))
    (command "pedit" "m" ss "" "y" "j" "0" "")
    (command "pedit" "m" ss "" "j" "0" "")))
    (princ))
     
    Tom Smith, Feb 21, 2005
    #5
  6. mnash

    mnash Guest

    Hey Bill, Jeff, I'm not having any luck with the lisps you provided
    I'll keep at it though
    thanks for your efforts
     
    mnash, Feb 21, 2005
    #6
  7. mnash

    Jeff Mishler Guest

    Hi Tom,
    I think I wrote this routine back when I wasn't sure how to do much of
    anything. I agree with the setting to nil part, however the nopoly does need
    to be set to run this correctly. Here's a snip from my history window, first
    running mine (minus the (setq nopoly nil) line) then your version:

    Command: pedit2

    Select Objects to Join Together.....
    Select objects: Specify opposite corner: 9 found

    Select objects:
    pedit Select polyline or [Multiple]: m
    Select objects: 9 found

    Select objects:
    Convert Lines and Arcs to polylines [Yes/No]? <Y> y
    Enter an option [Close/Open/Join/Width/Fit/Spline/Decurve/Ltype gen/Undo]: j
    Join Type = Extend
    Enter fuzz distance or [Jointype] <0.0000>: 0
    8 segments added to polyline

    Enter an option [Close/Open/Join/Width/Fit/Spline/Decurve/Ltype gen/Undo]:
    Command:
    Command: pedit2a
    Select Objects to Join Together.....
    Select objects: Specify opposite corner: 9 found

    Select objects:
    pedit Select polyline or [Multiple]: m
    Select objects: 9 found

    Select objects:
    Convert Lines and Arcs to polylines [Yes/No]? <Y> j
    Yes or No, please.
    ; error: Function cancelled

    Convert Lines and Arcs to polylines [Yes/No]? <Y> *Cancel*

    Enter an option [Close/Open/Join/Width/Fit/Spline/Decurve/Ltype gen/Undo]:
    *Cancel*

    The whole reason I placed that check in there is due to "pedit" "m" acting
    differently based on ALL the objects in the ss.
     
    Jeff Mishler, Feb 21, 2005
    #7
  8. mnash

    mnash Guest

    I just realized that it only works if the first entity selected is not a polyline, I'm lost, if the first entity is a polyline, it asks if i want to convert arc and line to polylines

    :(
     
    mnash, Feb 21, 2005
    #8
  9. Don't let PEDITACCEPT trip you up.

    --
    Autodesk Discussion Group Facilitator



     
    Jason Piercey, Feb 21, 2005
    #9
  10. mnash

    Jeff Mishler Guest

    Heh, that won't be a problem for me for the foreseeable
    future.......peditaccept is not a sysvar in rel. 2002 and my employer won't
    be spending the $ to upgrade anytime soon. But thanks for the heads up for
    those that are using the newer versions.

    --
    Jeff
    check out www.cadvault.com
     
    Jeff Mishler, Feb 21, 2005
    #10
  11. mnash

    Jeff Mishler Guest

    mnash,
    If you take the original routine I posted and remove the one line (setq
    nopoly nil), save and reload, it should work. I've tried it here with all
    polylines, no polylines and a mix of lines, arcs and plines and it works in
    all cases for me.

    If you are using r2004+, see the post that Jason Piercey made.
     
    Jeff Mishler, Feb 21, 2005
    #11
  12. mnash

    BillZ Guest

    Why not set PEDITACCEPT = 1.

    That way AutoCAD will not prompt you Y/N and automatically convert all entities into polylines.


    Bill
     
    BillZ, Feb 21, 2005
    #12
  13. mnash

    BillZ Guest

    Thanks Jason,

    It's taking me a while to adjust to all these new features.

    Bill
     
    BillZ, Feb 21, 2005
    #13
  14. mnash

    Tom Smith Guest

    however the nopoly does need to be set to run this correctly

    I can't see why. You're setting nopoly to one of two states. Depending on
    which it is, you're running either the "then" or the "else" clause of the
    if. The nopoly variable is redundant. Maybe I broke the code elsewhere, I
    don't know, but I'm quite certain you don't need that variable. You don't
    need two separate ifs to handle a single choice of two possibilities.

    Here's what you're doing, simplified into pseudocode:

    (if (negative-test)
    (setq notflag t)
    (setq notflag nil)
    )
    (if notflag
    (do-then)
    (do-else)
    )

    Which can be reduced to:

    (if (negative-test)
    (do-then)
    (do-else)
    )

    The nopoly variable isn't serving any purpose except to determine the
    outcome of the second (redundant) if.
     
    Tom Smith, Feb 21, 2005
    #14
  15. mnash

    Jeff Mishler Guest

    Tom,
    What you are missing is that I'm setting the true/false variable INSIDE of
    the (while) loop and then running ONE command based on that result.

    The way yours is structured, it will try to run the command on each and
    every entity in the selection set since it is inside the (while) loop, which
    may, or may not, be why yours is failing since once a line/arc is converted
    to a pline the ename is no longer valid.
     
    Jeff Mishler, Feb 22, 2005
    #15
  16. Try this:
    Code:
    ; -------------------------------------------------------------------------
    ; ALE_PlJoin.lsp - 22/02/2005
    ; Copyright ©2005 Marc'Antonio Alessi, Italy - All rights reserved
    ; http://xoomer.virgilio.it/alessi
    ; -------------------------------------------------------------------------
    ; #PlineJoinPrec = global
    ;
    (defun C:ALE_PlJoin (/ SelSet FltLst OldLPr)
    ;(setvar "CMDECHO" 0) ; I have always = 0
    (princ "\nSelect lines, arcs or polylines to join:  ")
    (setq OldLPr (getvar "LUPREC"))  (setvar "LUPREC" 8)
    (setq
    FltLst
    '((0 . "LWPOLYLINE,POLYLINE,LINE,ARC"))
    #PlineJoinPrec
    (ureal 5 "" "Precision distance" (cond ( #PlineJoinPrec ) ( 0.00001 )))
    )
    (setvar "LUPREC" OldLPr)
    (cond
    ( (not (setq SelSet (cond ((ssget "_I" FltLst)) ((ssget FltLst)))))
    (prompt "\nNo arc or polyline selected.  ")
    )
    ( (ssget "_P" '((0 . "LINE,ARC")))
    (command
    "_.PEDIT" "_M" SelSet "" "_Y" "_J" "_J" "_E" #PlineJoinPrec ""
    )
    (princ (strcat
    "\n" (itoa (sslength SelSet))
    " lines, arcs or polylines are joined.  "
    )       )
    )
    ( T
    (command "_.PEDIT" "_M" SelSet "" "_J" "_J" "_E" #PlineJoinPrec "")
    (princ "\n_ ")
    (princ
    (strcat "\n " (itoa (sslength SelSet)) " polylines are joined.")
    )
    )
    )
    (princ)
    )
    ;
    (defun ureal (bit kwd msg def / inp)
    (if def
    (setq
    msg (strcat "\n" msg " <" (ALE_RTOS_DZ8 def) ">: ")
    bit (* 2 (fix (/ bit 2)))
    )
    (setq msg (strcat "\n" msg ": "))
    )
    (initget bit kwd)
    (setq inp (getreal msg))
    (if inp inp def)
    )
    ;
    (defun ALE_RtoS_DZ8 (ReaVal / CurDZn OutVal)
    (if (= 8 (setq CurDZn (getvar "DIMZIN")))
    (setq CurDZn nil)
    (setvar "DIMZIN" 8)
    )
    (setq OutVal (rtos ReaVal 2))
    (and CurDZn (setvar "DIMZIN" CurDZn))
    OutVal
    )
    
     
    Marc'Antonio Alessi, Feb 22, 2005
    #16
  17. mnash

    mnash Guest

    Bill
    This was something I was unaware of, however thanks for thinking outside the box, now the lisp I have created is simple enough for me to understand.
    Thanks to you all have learned a lot from this discussion.
    Cheers all
    MN
     
    mnash, Feb 22, 2005
    #17
  18. mnash

    BillZ Guest

    You're welcome.

    But Jason thought of it first. :)


    Bill
     
    BillZ, Feb 22, 2005
    #18
  19. mnash

    Tom Smith Guest

    What you are missing is that I'm setting the true/false variable INSIDE of
    the (while) loop and then running ONE command based on that result.

    That's obvious, and I can't see why you won't understand what I'm saying.
    I'm not talking about a different way of structuring your program. I'm not
    talking about a competitive version of the same program. I'm saying that
    your nopoly variable, in your code, as you are using it, is redundant. It
    doesn't matter whether the redundancy happens inside a while loop.

    You have two ifs in a row, inside your loop. The last if determines which
    one of two command sequences to run on that iteration of the loop. How does
    this if decide which? Well, it decides based on the previous if, which sets
    the nopoly variable. So the first if sets one of two possible values of
    nopoly, and then the very next program event is another if, which runs one
    of two commands to based on which of the two values nopoly was given. You
    can eliminate the redundancy by combining the two if constructions into one.

    I tried to make this as apparent as possible in my pseudocode. Let's try
    again. What you are doing is this:

    (while (whatever)
    ;set nopoly flag to indicate pline status
    (if (not (polytest))
    (setq nopoly t)
    (setq nopoly nil)
    )
    ;run one of two commands based on nopoly flag status
    (if nopoly
    (nopolycommand)
    (polycommand)
    )
    )

    All I am saying is that you are using two separate successive if
    constructions to test a single condition and assign one of two results
    accordingly, and that you can do exactly the same thing in one single if.
    The following is exactly identical logic without the unnecessary variable:

    (while (whatever)
    ;run one of two commands based on pline status
    (if (not (polytest))
    (nopolycommand)
    (polycommand)
    )
    )
     
    Tom Smith, Feb 22, 2005
    #19
  20. mnash

    Jeff Mishler Guest

    Tom, I know what you are saying, and even understand it......the thing is
    you keep missing that my routine is not acting as you think it is. Look
    closely at your pseudo code, which BTW I already agreed that the (setq
    nopoly nil) should be removed:
    You placed the (if nopoly (blah...) INSIDE the while loop which would end up
    trying to run the (command "pedit".....) on each item in the selection set
    and you are RIGHT that that is stupid. However, my routine has that OUTSIDE
    the while loop so that one of two (command "pedit"....) lines, based on
    whether a non-pline object is included in the selection set, will run once
    and ONLY once on the entire selection set.

    So to sum it up:
    Run through the selection set and check for any objects that aren't plines.

    If none are found run the command one way, but if there are run command
    another way

    Does that make more sense? Although I thought that I had said that in my
    last post......
     
    Jeff Mishler, Feb 22, 2005
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.