Command-s use of EXPLODE not functioning

Command-s use of EXPLODE not functioning

Jonathan_Bell82GXG
Enthusiast Enthusiast
930 Views
10 Replies
Message 1 of 11

Command-s use of EXPLODE not functioning

Jonathan_Bell82GXG
Enthusiast
Enthusiast

I've got a subroutine that for the most part does what I want it to do (unioning together the green layer and unioning together the blue layer), but then I want to explode each resulting entity into its original polylines rather than the region that results from the union command. The subroutine does all this for just a single grouping (not in the sense of the GROUP command), but when I select multiple groupings it doesn't explode.

Jonathan_Bell82GXG_0-1692128425289.png

 

(defun c:smartTrim ( / )
  
  ; get ss of containing offset layers, split into 1 ss for each layer
  (setq ss (ssget '((8 . "PV Mech Equip Setback,PV Vents Skylights Roof Drains"))))
  (setq ssMech (ssdel (ssname (ssget "_L") 0) (ssget "_L")))
  (repeat (setq inc (sslength ss))
    (setq ent (entget (ssname ss (setq inc (1- inc)))))
    (if (equal (cdr (assoc '8 ent)) 
               "PV Mech Equip Setback"
        ) 
        (progn 
          (setq eName (cdr (assoc '-1 ent)))
          (ssadd eName ssMech)
          (ssdel eName ss)
        )
    )
  )
  
  (setq ssMisc ss)
  (setq ss nil)
  
  ; trimming PV VENTS SKYLIGHTS ROOF DRAINS
  (command-s "_.-LAYER" "_SET" "PV Vents Skylights Roof Drains" "")
  
  (setq ssMiscRegion (ssdel (ssname (ssget "_L") 0) (ssget "_L")))
  (repeat (setq inc (sslength ssMisc))
    (setq ent (entget (ssname ssMisc (setq inc (1- inc)))))
    (setq eName (cdr (assoc -1 ent)))
    
    (command-s "_.REGION" eName "")
        
    (setq newEname (entlast))
    (ssadd newEname ssMiscRegion)
    (ssdel eName ssMisc)
  )
  
  (setq ssMisc nil)

  (command-s "_.UNION" ssMiscRegion "")
  (setq regionToExplode (entlast))
  (command-s "_.EXPLODE" regionToExplode "") ;;; HERE - explode not working
  
  ; trimming PV MECH EQUIP SETBACK
  (command-s "_.-LAYER" "_SET" "PV Mech Equip Setback" "")
  
  (setq ssMechRegion (ssdel (ssname (ssget "_L") 0) (ssget "_L")))
  (repeat (setq inc (sslength ssMech))
    (setq ent (entget (ssname ssMech (setq inc (1- inc)))))
    (setq eName (cdr (assoc -1 ent)))
    
    (command-s "_.REGION" eName "")
        
    (setq newEname (entlast))
    (ssadd newEname ssMechRegion)
    (ssdel eName ssMech)
  )
  
  (setq ssMech nil)

  (command-s "_.UNION" ssMechRegion "")
  (setq regionToExplode (entlast))
  (command-s "_.EXPLODE" regionToExplode "") ;;; HERE - explode not working
  
  ; exit quietly
  (princ)
)

 

Any suggestions?

 

0 Likes
931 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

perhaps add (initcommandversion) predecessor

 

(defun c:smartTrim ( / )
  
  ; get ss of containing offset layers, split into 1 ss for each layer
  (setq ss (ssget '((8 . "PV Mech Equip Setback,PV Vents Skylights Roof Drains"))))
  (setq ssMech (ssdel (ssname (ssget "_L") 0) (ssget "_L")))
  (repeat (setq inc (sslength ss))
    (setq ent (entget (ssname ss (setq inc (1- inc)))))
    (if (equal (cdr (assoc '8 ent)) 
               "PV Mech Equip Setback"
        ) 
        (progn 
          (setq eName (cdr (assoc '-1 ent)))
          (ssadd eName ssMech)
          (ssdel eName ss)
        )
    )
  )
  
  (setq ssMisc ss)
  (setq ss nil)
  
  ; trimming PV VENTS SKYLIGHTS ROOF DRAINS
  (command-s "_.-LAYER" "_SET" "PV Vents Skylights Roof Drains" "")
  
  (setq ssMiscRegion (ssdel (ssname (ssget "_L") 0) (ssget "_L")))
  (repeat (setq inc (sslength ssMisc))
    (setq ent (entget (ssname ssMisc (setq inc (1- inc)))))
    (setq eName (cdr (assoc -1 ent)))
    
    (command-s "_.REGION" eName "")
        
    (setq newEname (entlast))
    (ssadd newEname ssMiscRegion)
    (ssdel eName ssMisc)
  )
  
  (setq ssMisc nil)

  (command-s "_.UNION" ssMiscRegion "")
  (setq regionToExplode (entlast))
  (initcommandversion)
  (command-s "_.EXPLODE" regionToExplode "") ;;; HERE - explode not working
  
  ; trimming PV MECH EQUIP SETBACK
  (command-s "_.-LAYER" "_SET" "PV Mech Equip Setback" "")
  
  (setq ssMechRegion (ssdel (ssname (ssget "_L") 0) (ssget "_L")))
  (repeat (setq inc (sslength ssMech))
    (setq ent (entget (ssname ssMech (setq inc (1- inc)))))
    (setq eName (cdr (assoc -1 ent)))
    
    (command-s "_.REGION" eName "")
        
    (setq newEname (entlast))
    (ssadd newEname ssMechRegion)
    (ssdel eName ssMech)
  )
  
  (setq ssMech nil)

  (command-s "_.UNION" ssMechRegion "")
  (setq regionToExplode (entlast))
  (initcommandversion)
  (command-s "_.EXPLODE" regionToExplode "") ;;; HERE - explode not working
  
  ; exit quietly
  (princ)
)
0 Likes
Message 3 of 11

ВeekeeCZ
Consultant
Consultant

BTW this construct is ridiculous

(setq ssMech (ssdel (ssname (ssget "_L") 0) (ssget "_L")))

 

just use 

(setq ssMech (ssadd))

it creates an empty selection set. 

 

Also, in general, I would rather use simple (command) instead of (command-s)... 

0 Likes
Message 4 of 11

Jonathan_Bell82GXG
Enthusiast
Enthusiast
Thanks for the input on making empty selection sets haha. Isn't command-s faster than command though? What are the downsides of command-s aside from less flexibility in how you use the command function?
0 Likes
Message 5 of 11

Jonathan_Bell82GXG
Enthusiast
Enthusiast

(initcommandversion) didn't change anything-- still doesn't explode entities in the second case

0 Likes
Message 6 of 11

ronjonp
Advisor
Advisor

When in doubt remove command calls:

(vla-explode (vlax-ename->vla-object regiontoexplode))
(entdel regiontoexplode)
0 Likes
Message 7 of 11

Jonathan_Bell82GXG
Enthusiast
Enthusiast
Still nothing. Is there something about REGIONs that make this more difficult?
0 Likes
Message 8 of 11

ronjonp
Advisor
Advisor

@Jonathan_Bell82GXG wrote:
Still nothing. Is there something about REGIONs that make this more difficult?

No ... do you read the Autodesk documentation? HERE.

ronjonp_0-1692235751306.png

 

0 Likes
Message 9 of 11

ВeekeeCZ
Consultant
Consultant

@Jonathan_Bell82GXG wrote:
Still nothing. Is there something about REGIONs that make this more difficult?

 

Yes. You need to explore how those commands work. Mainly whether creates a new entity or just changes the original. Use (entsel) for that. UNION does not create a new entity!

 

Also, EXPLODE is tricky when comes to regions. It creates a polyline only if it is one object. Else create regions.

 

Your routine fixed. 

(defun c:smartTrim ( / ss )
  
  ; get ss of containing offset layers, split into 1 ss for each layer
  (setq ss (ssget '((8 . "PV Mech Equip Setback,PV Vents Skylights Roof Drains"))))

  (setq ssMech (ssadd))

  (repeat (setq inc (sslength ss))
    (setq ent (ssname ss (setq inc (1- inc))))
    (if (= (cdr (assoc 8 (entget ent))) "PV Mech Equip Setback")
      (progn
	(ssadd ent ssMech)
	(ssdel ent ss))))
  
  
  (setq ssMisc ss)
  
  ; trimming PV VENTS SKYLIGHTS ROOF DRAINS
  (command-s "_.-LAYER" "_Make" "PV Vents Skylights Roof Drains" "")
  
  (setq ssMiscRegion (ssadd))
  (repeat (setq inc (sslength ssMisc))
    (setq ent (entget (ssname ssMisc (setq inc (1- inc)))))
    (setq eName (cdr (assoc -1 ent)))
    
    (command-s "_.REGION" eName "")
        
    (setq newEname (entlast))
    (ssadd newEname ssMiscRegion)
    (ssdel eName ssMisc)
  )
  
  (setq ssMisc nil)

  (command-s "_.UNION" ssMiscRegion "")
  (initcommandversion)
  (command "_.EXPLODE" "_p" "") ;;; HERE - explode not working
  (initcommandversion)
  (command "_.JOIN" "_p" "")
  
  ; trimming PV MECH EQUIP SETBACK
  (command-s "_.-LAYER" "_SET" "PV Mech Equip Setback" "")
  
  (setq ssMechRegion (ssdel (ssname (ssget "_L") 0) (ssget "_L")))
  (repeat (setq inc (sslength ssMech))
    (setq ent (entget (ssname ssMech (setq inc (1- inc)))))
    (setq eName (cdr (assoc -1 ent)))
    
    (command-s "_.REGION" eName "")
        
    (setq newEname (entlast))
    (ssadd newEname ssMechRegion)
    (ssdel eName ssMech)
  )
  
  (setq ssMech nil)

  (command-s "_.UNION" ssMechRegion "")
  (initcommandversion)
  (command-s "_.EXPLODE" "_p" "")
  (initcommandversion)
  (command "_.JOIN" "_p" "")
  ; exit quietly
  (princ)
)

 

 

But it would be much faster, if you supply REGION with a selection set (all at once), like this.

(defun c:SmartTrim ( / *error* s i e y l N)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if lay (setvar 'clayer lay))
    (if del (setvar 'delobj del))
    (princ))
  
  (setq lay (getvar 'clayer))
  (setq del (getvar 'delobj)) ; ?? set to what
  
  (if (setq s (ssget '((-4 . "<OR") (-4 . "<AND") (0 . "LWPOLYLINE") (-4 . "&=") (70 . 1) (-4 . "AND>") (0 . "CIRCLE") (-4 . "OR>"))))
    
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i)))
	    y (cdr (assoc 8 (entget e)))
	    l (if (setq a (assoc y l))
		(subst (cons y (ssadd e (cdr a))) a l)
		(cons  (cons y (ssadd e)) l)))))
  
  (foreach i l
    
    (setq e (entlast) n (ssadd))
    (setvar 'clayer (car i))
    (command "_.region" (cdr i) "")
    (while (setq e (entnext e)) (ssadd e n))
       
    (command "_.union" n "")
    (initcommandversion)
    (command "_.explode" "_p" "")
    (initcommandversion)
    (command "_.join" "_p" "")
    (if (setq n (ssget "_p" '((0 . "REGION")))) 
      (command "_.erase" n ""))
    )
  
  (*error* "end")
  )

 

0 Likes
Message 10 of 11

ВeekeeCZ
Consultant
Consultant

@Jonathan_Bell82GXG wrote:
Thanks for the input on making empty selection sets haha. Isn't command-s faster than command though? What are the downsides of command-s aside from less flexibility in how you use the command function?

 

Isn't command-s faster than command though?

Not sure about that. Why do you think so? 

 

 

The main issue (or advantage it you know what are you doing) is that command-s does not leave the command if an error occurs and then continues in evaluation of the following code (this could be very risky).

The command stops evaluation and goes straight to the *error* function.

 

 

0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

@Jonathan_Bell82GXG wrote:
....  What are the downsides of command-s aside from less flexibility in how you use the command function?

Read about the differences between the two in >Help for the (command-s) function<.

Kent Cooper, AIA
0 Likes