Issue With Foreach in my Function - Attempting to Connect Lines Based on Layer

Issue With Foreach in my Function - Attempting to Connect Lines Based on Layer

Anonymous
Not applicable
881 Views
3 Replies
Message 1 of 4

Issue With Foreach in my Function - Attempting to Connect Lines Based on Layer

Anonymous
Not applicable

Good morning/afternoon/evening,

 

I am relatively new to using AutoLisp but I am interning to help my department to transition into using is more. Any and all help is greatly appreciated! I have been using this forum extensively for problem solving and I felt I finally had a problem that you all might be able to help with.

 

Background:

Currently, I am attempting to write a function that can be used either while working on a file or while running ScriptPro. The idea of this function is that you select your items and then call the function (or you could call the function and then select the items). The function itself will then consider lines based on their layer and attempt to join them (ensures that only like layers are joined). The reason I separate my Pedit and MPedit is because I am experiencing an issue that Pedit seems to ignore my fuzz distance while MPedit does not (we keep the Pedit in because of the conversion to a polyline).

 

Problem Statement:

The foreach in my function doesn't seem to work the way I want it to (assuming I understand how it works). Currently, the program seems to run through each layer first and assigns it into a list, but it then proceeds to attempt the Pedit and MPedit (which means it pretty much skips to the last item in the list for joining). In addition, I attempt in the MPEDIT script to use the previous selection because I assumed the Pedit would have added its work to a selection set (which doesn't seem to always be the case).

 

The princ statements are only for debugging and I will include sample output below (far bottom) as well as a sample drawing that I have been using to try and debug with (includes my expected results).

 

Lisp Function (This was customized off of a solution Lee Mac gave here!)

(defun c:PJL2 ( / ent idx itm lay lst sel val var )
    (setq var '(peditaccept cmdecho)
          val  (mapcar 'getvar var)
    )
    (mapcar '(lambda ( a b c ) (if a (setvar b c))) val var '(1 0))
    (if (setq sel (ssget "_:L" '((0 . "ARC,LINE,LWPOLYLINE"))))
        (progn
            (repeat (setq idx (sslength sel))
                (setq ent (ssname sel (setq idx (1- idx)))
                      lay (cdr (assoc 8 (entget ent)))
                )
                (if (setq itm (assoc lay lst))
                    (setq lst (subst (vl-list* lay ent (cdr itm)) itm lst))
                    (setq lst (cons  (list lay ent) lst))
                )
            )

     (setq ind 0)
     (foreach grp lst
         (setq templst (nth ind lst))

         (princ templst)

         (princ "\n")

         (setq newtemplst (nth 0 templst))

         (setvar "clayer" newtemplst)

         (princ newtemplst)

         (setq ind (+ ind 1))

         (princ "\n")

         (command "_-LAYER" "_F" "*" "")

         (apply 'command (append '("_.pedit" "_m") (cdr grp) '("" "_j" "J" "E" "0" "")))

         (apply 'command (append '("script" "C:/Users/routercim3/Desktop/NICK'S PROJECTS/ScriptPro/Script Code/SPROMPEDIT.scr")))
         (command "_-Layer" "THAW" "*" "")
     )
        )
    )
    (mapcar '(lambda ( a b ) (if b (setvar a b))) var val)
    (princ)
)

 

 

Script File (SPROMPEDIT.scr):

_.MPEDIT
_PRE

_N
_J
_J
_B
0.07

 

 

Sample Command Line Output:

Command: Specify opposite corner or [Fence/WPolygon/CPolygon]:
Command: PJL2
70 found
(0625DR_B <Entity name: 1f91e7b2b60> <Entity name: 1f91e7b2b40> <Entity name: 1f91e7b2b30> <Entity name: 1f91e7b2b20> <Entity name: 1f91e7b2b10> <Entity name: 1f91e7b2b00> <Entity name: 1f91e7b2af0> <Entity name: 1f91e7b2ae0> <Entity name: 1f91e7b2ad0> <Entity name: 1f91e7b2ac0> <Entity name: 1f91e7b2ab0> <Entity name: 1f91e7b2aa0> <Entity name: 1f91e7b2a90> <Entity name: 1f91e7b2a80>)
0625DR_B
(SMSLOT <Entity name: 1f91e7afcd0> <Entity name: 1f91e7afcc0> <Entity name: 1f91e7afcb0>)
SMSLOT
(LGRAB <Entity name: 1f91e7afca0> <Entity name: 1f91e7afc90> <Entity name: 1f91e7afc80> <Entity name: 1f91e7afc70> <Entity name: 1f91e7afc60> <Entity name: 1f91e7afc50> <Entity name: 1f91e7afc40> <Entity name: 1f91e7afc20> <Entity name: 1f91e7afc10> <Entity name: 1f91e7afc00> <Entity name: 1f91e7afbf0> <Entity name: 1f91e7afbe0>)
LGRAB
(45DEG <Entity name: 1f91e7b2a70> <Entity name: 1f91e7afbd0> <Entity name: 1f91e7afbc0> <Entity name: 1f91e7afbb0> <Entity name: 1f91e7afba0> <Entity name: 1f91e7afb90> <Entity name: 1f91e7afb80> <Entity name: 1f91e7afb70> <Entity name: 1f91e7afb60> <Entity name: 1f91e7afaa0> <Entity name: 1f91e7afa90> <Entity name: 1f91e7afa80> <Entity name: 1f91e7afa70> <Entity name: 1f91e7afa60> <Entity name: 1f91e7afa50> <Entity name: 1f91e7afa40> <Entity name: 1f91e7afa30> <Entity name: 1f91e7af9a0> <Entity name: 1f91e7af990> <Entity name: 1f91e7af980> <Entity name: 1f91e7af970> <Entity name: 1f91e7af960> <Entity name: 1f91e7af950> <Entity name: 1f91e7af940> <Entity name: 1f91e7af930> <Entity name: 1f91e7af920> <Entity name: 1f91e7af910> <Entity name: 1f91e7af900> <Entity name: 1f91e7af8e0> <Entity name: 1f91e7af8d0> <Entity name: 1f91e7af8c0> <Entity name: 1f91e7af8b0> <Entity name: 1f91e7af8a0> <Entity name: 1f91e7af890> <Entity name: 1f91e7af880> <Entity name: 1f91e7af870> <Entity name: 1f91e7af860> <Entity name: 1f91e7af850> <Entity name: 1f91e7af840> <Entity name: 1f91e7af830> <Entity name: 1f91e7adf70>)
45DEG
Command: _.MPEDIT
Initializing...
Select objects: _PRE
No previous selection set.
Select objects:
Nothing selected.
Command: _N
Unknown command "N".  Press F1 for help.
Command: *Cancel*

0 Likes
Accepted solutions (1)
882 Views
3 Replies
Replies (3)
Message 2 of 4

ВeekeeCZ
Consultant
Consultant
Accepted solution

Just tried to simplify it a bit. Try and let know.

 

(defun c:PJL2 ( / :acet-pljoin ent idx itm lay lst sel val var ssp ssl enl)
  
  (defun :acet-pljoin (ss stl fuz / )
    (load (strcat (vl-registry-read (strcat "HKEY_LOCAL_MACHINE\\" (vlax-product-key)) "ACADLOCATION") "\\Express\\" "pljoin.lsp") "Express Tools Reqiured!")
    (load "pljoinsup.lsp" "Express Tools Reqiured!")
    (acet-pljoin2 ss stl fuz))
  
  
  (setq var '(peditaccept cmdecho)
        val  (mapcar 'getvar var))
  
  (mapcar '(lambda ( a b c ) (if a (setvar b c))) val var '(1 0))
  
  (if (setq sel (ssget "_:L" '((0 . "ARC,LINE,LWPOLYLINE"))))
    (repeat (setq idx (sslength sel))
      (setq ent (ssname sel (setq idx (1- idx)))
            lay (cdr (assoc 8 (entget ent)))
            )
      (if (setq itm (assoc lay lst))
        (setq lst (subst (vl-list* lay ent (cdr itm)) itm lst))
        (setq lst (cons  (list lay ent) lst)))))
  
  (foreach grp lst
    
    (setq ssp (ssadd)
          ssl (ssadd)
          enl (entlast))
    
    (foreach ent (cdr grp)
      (ssadd ent (if (= "LWPOLYLINE" (cdr (assoc 0 (entget ent))))
                   ssp
                   ssl)))

    (command "_.pedit" "_m" ssl "") (command)
    
    (while (setq enl (entnext enl))
      (ssadd enl ssp))
    
    (if (> (sslength ssp) 1) (:acet-pljoin ssp "Both" 0.07)))
  
  (mapcar '(lambda ( a b ) (if b (setvar a b))) var val)
  (princ)
  )

 

 

0 Likes
Message 3 of 4

Anonymous
Not applicable

BeekeeCZ,

 

Code worked beautifully! Thank you very much for the assistance!

 

Definitely going to look closer at this and see what I can learn.

 

Have a great day,

Nicholas English

0 Likes
Message 4 of 4

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

....

Command: _.MPEDIT
Initializing...
Select objects: _PRE
No previous selection set.
Select objects:
Nothing selected.
Command: _N
Unknown command "N".  Press F1 for help.
Command: *Cancel*


 

Now I see this listing... kinda confirmed my thoughts.

 

The issue is that PEDIT by "converting" lines/arcs to polylines is actually creating new entities. That said, a previous selection set (= list of enames) is not valid anymore. You need to collect all newly created entities, as I did. And since polylines don't need any conversion... that's why I created 2 selection set. One for polylines (ssp), the second for lines/arcs (ssl). Once the lines are converted, their new enames are added to ssp. 

HTH

0 Likes