Bypass user selection in LISP to convert Region to Polyline

Bypass user selection in LISP to convert Region to Polyline

Anonymous
Not applicable
5,034 Views
8 Replies
Message 1 of 9

Bypass user selection in LISP to convert Region to Polyline

Anonymous
Not applicable

Hi folks, 

 

I would like to ByPass the user input for selection of Regions to be converted to Polyline in the following LISP:

 

(defun c:Region2Polyline nil
(if (setq ss (ssget '((0 . "REGION"))))
(:Region2Polyline ss))
(princ)
)

;; Gilles Chanteau- 01/01/07
(defun :Region2Polyline (ss / *error* arcbugle acdoc space
n reg norm expl olst blst dlst plst tlst blg pline)

;-----
(defun *error* (msg)
(if (/= msg "Function cancelled")
(princ (strcat "\nError: " msg)))
(vla-EndUndoMark (vla-get-ActiveDocument (vlax-get-acad-object)))
(princ))

;-----
(defun arcbulge (arc)
(/ (sin (/ (vla-get-TotalAngle arc) 4))
(cos (/ (vla-get-TotalAngle arc) 4))))

;-----
;-----

(setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object))
space (if (= 1 (getvar "CVPORT"))
(vla-get-PaperSpace acdoc)
(vla-get-ModelSpace acdoc)))
(if ss
(repeat (setq i (sslength ss))
(setq reg (vlax-ename->vla-object (ssname ss (setq i (1- i))))
norm (vlax-get reg 'Normal)
expl (vlax-invoke reg 'Explode))
(if (vl-every '(lambda (x) (or (= (vla-get-ObjectName x) "AcDbLine")
(= (vla-get-ObjectName x) "AcDbArc")))
expl)
(progn
(vla-delete reg)
(setq olst (mapcar '(lambda (x) (list x (vlax-get x 'StartPoint) (vlax-get x 'EndPoint)))
expl))
(while olst
(setq blst nil)
(if (= (vla-get-ObjectName (caar olst)) "AcDbArc")
(setq blst (list (cons 0 (arcbulge (caar olst))))))
(setq plst (cdar olst)
dlst (list (caar olst))
olst (cdr olst))
(while (setq tlst (vl-member-if '(lambda (x) (or (equal (last plst) (cadr x) 1e-9)
(equal (last plst) (caddr x) 1e-9)))
olst))
(if (equal (last plst) (caddar tlst) 1e-9)
(setq blg -1)
(setq blg 1))
(if (= (vla-get-ObjectName (caar tlst)) "AcDbArc")
(setq blst (cons (cons (1- (length plst))
(* blg (arcbulge (caar tlst)))
)
blst)))
(setq plst (append plst
(if (minusp blg)
(list (cadar tlst))
(list (caddar tlst))))
dlst (cons (caar tlst) dlst)
olst (vl-remove (car tlst) olst)))
(setq pline (vlax-invoke Space 'addLightWeightPolyline (apply 'append (mapcar '(lambda (x)
(setq x (trans x 0 Norm))
(list (car x) (cadr x)))
(reverse (cdr (reverse plst)))))))
(vla-put-Closed pline :vlax-true)
(mapcar '(lambda (x) (vla-setBulge pline (car x) (cdr x))) blst)
(vla-put-Elevation pline (caddr (trans (car plst) 0 Norm)))
(vla-put-Normal pline (vlax-3d-point Norm))
(mapcar 'vla-delete dlst)))
(mapcar 'vla-delete expl)))
)
)
(c:Region2Polyline)

 

Is there any way to just load this LISP, being executed for ALL regions without any user input? I've added "(c:Region2Polyline)", but it only loads the command still needing the user to type "all" to select all regions to be converted to polylines. I would like to ByPass this extra input. Thanks!

 

0 Likes
Accepted solutions (1)
5,035 Views
8 Replies
Replies (8)
Message 2 of 9

Satish_Rajdev
Advocate
Advocate
Accepted solution

This will select regions automatically when you enter command and convert it to polylines.

(defun c:Region2Polyline nil
  (if (setq ss (ssget "_x" '((0 . "REGION"))))
    (:Region2Polyline ss)
  )
  (princ)
)

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes
Message 3 of 9

Anonymous
Not applicable

 


@Satish_Rajdev wrote:

This will select regions automatically when you enter command and convert it to polylines.

(defun c:Region2Polyline nil
  (if (setq ss (ssget "_x" '((0 . "REGION"))))
    (:Region2Polyline ss)
  )
  (princ)
)

Sorry, that was a ridiculous question, but it did exactly what I wanted! I'm trying to learn LISP. Robot Frustrated

So the "_x" is what made this LISP to get ANY and ALL regions in the drawing to be converted to Polylines? When I want to by-pass user selection and get all entities from same "thing", do I only need to put that "_x" in the LISP code, which is like a general "catch all"? Thanks a lot!

0 Likes
Message 4 of 9

cadffm
Consultant
Consultant
Start Help[F1]
Search for SSGET
to learn more about ssget.

Sebastian

0 Likes
Message 5 of 9

Anonymous
Not applicable

Thanks for the tip! I will use that as learning resource aswell.

0 Likes
Message 6 of 9

Satish_Rajdev
Advocate
Advocate

Automatic and manual filter can be apply on ssget function. "_x" is automatic filter in ssget function which automatically selects all entity present in space, you can also add entity filter like '((0 .  "region")) .

*Note - Manual filter means it asks user to select entity.

 

You can get more information about ssget function on automatic and manual filter here:

http://www.lee-mac.com/ssget.html

 

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes
Message 7 of 9

MehtaRajesh
Advocate
Advocate

Hi
I am getting "Error: AutoCAD.Application: Invalid argument coordinates in Coordinates property"

on one of the geometry while running  below code (extracted from your main code)

(setq pline (vlax-invoke Space 'addLightWeightPolyline
(apply 'append (mapcar '(lambda (x)
(setq x (trans x 0 Norm))
(list (car x) (cadr x))
)
(reverse (cdr (reverse plst)))
)

What could be the reason and the solution if any ?
Thanks in advance, regards  Raj




0 Likes
Message 8 of 9

marko_ribar
Advisor
Advisor

@MehtaRajesh wrote:

Hi
I am getting "Error: AutoCAD.Application: Invalid argument coordinates in Coordinates property"

on one of the geometry while running  below code (extracted from your main code)

(setq pline (vlax-invoke Space 'addLightWeightPolyline
(apply 'append (mapcar '(lambda (x)
(setq x (trans x 0 Norm))
(list (car x) (cadr x))
)
(reverse (cdr (reverse plst)))
)

What could be the reason and the solution if any ?
Thanks in advance, regards  Raj





Perhaps, you can try little different... Maybe with (command) calls... Something like this :

 

(defun c:sss nil
  (if (and sss (= (type sss) 'pickset))
    (sssetfirst nil sss)
  )
  (princ)
)

(defun c:regs2lws ( / *error* LM:popup pea cmde ss si i reg n k m p el ucsf )

  (vl-load-com)

  (defun *error* ( m )
    (if (= 8 (logand 8 (getvar 'undoctl)))
      (command-s "_.UNDO" "_E")
    )
    (if pea
      (setvar 'peditaccept pea)
    )
    (if cmde
      (setvar 'cmdecho cmde)
    )
    (if (and sss (= (type sss) 'pickset) (/= (sslength sss) 0))
      (sssetfirst nil sss)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  ;; Popup  -  Lee Mac
  ;; A wrapper for the WSH popup method to display a message box prompting the user.
  ;; ttl - [str] Text to be displayed in the pop-up title bar
  ;; msg - [str] Text content of the message box
  ;; bit - [int] Bit-coded integer indicating icon & button appearance
  ;; Returns: [int] Integer indicating the button pressed to exit

  (defun LM:popup ( ttl msg bit / wsh rtn )
    (if (setq wsh (vlax-create-object "wscript.shell"))
      (progn
        (setq rtn (vl-catch-all-apply 'vlax-invoke-method (list wsh 'popup msg 0 ttl bit)))
        (vlax-release-object wsh)
        (if (not (vl-catch-all-error-p rtn)) rtn)
      )
    )
  )

  (setq pea (getvar 'peditaccept))
  (setvar 'peditaccept 1)
  (setq cmde (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (if (= 8 (logand 8 (getvar 'undoctl)))
    (vl-cmdf "_.UNDO" "_E")
  )
  (vl-cmdf "_.UNDO" "_G")
  (if (ssget "_A" (list '(0 . "REGION") '(60 . 0) (cons 410 (if (= (getvar 'cvport) 1) (getvar 'ctab) "Model"))))
    (progn
      (prompt "\nSelect REGION(S) to convert them to LWPOLYLINE(S)...")
      (if (setq ss (ssget "_:L" '((0 . "REGION"))))
        (progn
          (setq k 0 m 0 sss (ssadd))
          (repeat (setq i (sslength ss))
            (setq reg (ssname ss (setq i (1- i))))
            (setq n (vlax-safearray->list (vlax-variant-value (vla-get-normal (vlax-ename->vla-object reg)))))
            (if (= 0 (getvar 'worlducs))
              (progn
                (vl-cmdf "_.UCS" "_W")
                (setq ucsf t)
              )
            )
            (setq el (entlast))
            (vl-cmdf "_.EXPLODE" reg)
            (while (< 0 (getvar 'cmdactive))
              (vl-cmdf "")
            )
            (if (eq el (setq el (entlast)))
              (progn
                (prompt "\nExploding REGION entity didn't produce desired results... No new entities appended to database... Something is wrong, quitting...")
                (exit)
              )
            )
            (if (vl-every '(lambda ( x / xx ) (or (= (cdr (assoc 0 (setq xx (entget x)))) "LINE") (= (cdr (assoc 0 xx)) "ARC")))
                  (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget "_P"))))
                )
              (progn
                (setq m (1+ m))
                (setq p (vlax-curve-getstartpoint el))
                (vl-cmdf "_.UCS" "_M" p)
                (vl-cmdf "_.UCS" "_ZA" "" n)
                (vl-cmdf "_.PEDIT" "_M" (ssget "_P") "" "_J")
                (while (> (getvar 'cmdactive) 0)
                  (vl-cmdf "")
                )
                (if (eq el (setq el (entlast)))
                  (progn
                    (prompt "\nJoining enities after exploding REGION entity didn't produce desired results... No new LWPOLYLINE entity appended to database... Something is wrong, quitting...")
                    (exit)
                  )
                  (ssadd el sss)
                )
                (vl-cmdf "_.UCS" "_P")
                (vl-cmdf "_.UCS" "_P")
              )
              (progn
                (setq k (1+ k))
                (vl-cmdf "_.UNDO" "1")
              )
            )
            (if ucsf
              (progn
                (vl-cmdf "_.UCS" "_P")
                (setq ucsf nil)
              )
            )
          )
          (if (/= m 0)
            (if (= m 1)
              (prompt "\nOnly one REGION converted to LWPOLYLINE...")
              (prompt (strcat "\n" (itoa m) " REGIONS converted to LWPOLYLINES..."))
            )
          )
          (if (/= k 0)
            (if (= k 1)
              (prompt "\nOnly one REGION not possible to be converted to LWPOLYLINE...")
              (prompt (strcat "\n" (itoa k) " REGIONS not possible to be converted to LWPOLYLINES..."))
            )
          )
          (if (/= m 0)
            (progn
              (prompt "\nSelection set is stored in global variable : \"sss\"... You can call it with (c:sss), or (sssetfirst nil sss)...")
              (prompt "\nAfter using variable \"sss\" it is strongly advicable (if you wish to properly use another routine that uses \"sss\" variable), that you clear it by typing (setq sss nil), or even more radical - save DWG and reopen it [ if you have installed VBA Object Enabler for AutoCAD - use command : REVERT ]...")
            )
          )
          (if (or (/= m 0) (/= k 0))
            (textscr)
          )
        )
        (progn
          (prompt "\nNothing selected...")
          (if (= 4 (LM:popup "REGION(S) TO LWPOLYLINE(S) CONVERSION" "Choose option : " 53))
            (c:regs2lws)
          )
        )
      )
    )
    (prompt "\nNo visible REGION entities detected in current active layout/space... Unable to apply conversion task if no valid entites present for selection...")
  )
  (*error* nil)
)

 

HTH.

Regards, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 9 of 9

MehtaRajesh
Advocate
Advocate

Thanks a lot  for your quick reply,
I found after further debugging  that particular entity is having only two coordinate (which should not be the case),
Hence i excluded that entity from process, hence i am able to run my function bypassing unwanted entity.

0 Likes