Author Topic: Function if with entget confusion  (Read 5564 times)

0 Members and 1 Guest are viewing this topic.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Function if with entget confusion
« on: July 03, 2010, 03:33:39 PM »
hello everbody,
I have a confusion between LWPOLYLINE and POLYLINE as the following;
Code:
(setq ent1 (car(entsel"\nSelect Polyline:"))
   ent2 (entget ent1)
   poly (cdr(assoc 0 ent2))
   )
How to get the Entity name correctly for polylines ????
Code:
 
(if (= (or (LWPOLYLINE)(POLYLINE)) poly);;; <-- it is not correct
or like this
Code:
 (if (= "LWPOLYLINE,POLYLINE" poly);;; <-- it is not correct
Or like this
Code:
 (if (= "*POLYLINE" poly);;; <-- it is not correct
What is the correct selection for all types of polylines ... ?

Best regards.

Tharwat

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Function if with entget confusion
« Reply #1 on: July 03, 2010, 04:08:45 PM »
See if this helps you out:

Code: [Select]
(setq ss (ssget ":s" '((0 . "*polyline"))));;filter selection
(setq ent (ssname ss 0));;grab ename

;;check for entsel selection
(if (wcmatch (cdr (assoc 0 (entget ent))) "*POLYLINE")
  (alert "Im a happy polyline")
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Function if with entget confusion
« Reply #2 on: July 03, 2010, 04:32:58 PM »
Code: [Select]
(if (member poly '( "LWPOLYLINE" "POLYLINE" ))or
Code: [Select]
(if (vl-position poly '( "LWPOLYLINE" "POLYLINE" ))or
Code: [Select]
(if (wcmatch poly  "*POLYLINE" )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Function if with entget confusion
« Reply #3 on: July 03, 2010, 08:16:27 PM »
Code: [Select]
(if (or (= poly "LWPOLYLINE")  (= poly "POLYLINE"))
or using the following fentsel function (Filtered ENTity SELection)
Code: [Select]
(setq ent1 (car (fentsel "\nSelect Polyline: " "LWPOLYLINE,POLYLINE")))or
Code: [Select]
(setq ent1 (car (fentsel "\nSelect Polyline: " "*POLYLINE")))
Code: [Select]
;; fentsel
;; Works like the entsel function with an entity type filter
;; Returns a list (ename PickedPoint) or nil if none entity or an incorrect one was selected.
;;
;; Arguments
;; msg: (string) a message (nil or "" for default).
;; fltr: (string) the entity type filter. Not case sensitive, accepts wild-card patterns.

(defun fentsel (msg fltr / ent)
  (if
    (and
      (setq ent (if (and (= (type msg) 'STR) (/= msg ""))
  (entsel msg)
  (entsel)
)
      )
      (wcmatch (cdr (assoc 0 (entget (car ent)))) (strcase fltr))
    )
     ent
  )
)
« Last Edit: July 03, 2010, 08:23:05 PM by gile »
Speaking English as a French Frog

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Function if with entget confusion
« Reply #4 on: July 04, 2010, 02:12:00 AM »
Thanks for all.
But I still have a confusion by suing the ssget and entget . and the following Lisp is not working well.
Any clarification to the theme would be highly appreciated......
Code: [Select]
(defun c:chsg (/ ss poly entNew sgmntOld sgmntNew sgNew)
(setq ss (ssget ":s" '((0 . "*polyline"))))
(setq poly (cdr (assoc 0 (entget ss)))) 
 (if (member poly '( "LWPOLYLINE" "POLYLINE" ))
 (setq entNew (getint"\nSpecify New width of Polyline 1st Segment:")
       sgmntOld (assoc 40 poly)
       sgmntNew (cons 40 entNew)
       sgNew (subst sgmntNew sgmntOld poly)
     )
     (alert (strcat "Your Selection is :" "  " (cdr (assoc 0 poly))"."))
          )
  (entmod sgNew)
   (princ)
  )

Tharwat
   

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Function if with entget confusion
« Reply #5 on: July 04, 2010, 04:30:26 AM »
Did you read the AutoCAD Developer's Help for the ssget, entsel, entget functions ?

You have to always ask yourself: "what data type the function I'm using requires and what data type it returns ?"
for example:
entget requires an ENAME
ssget returns a PICKSET (a selection set is a quite complex object containing ENAMEs and informations about how it was created)
entsel returns a list which first item is an ENAME and second a point (a list of 3 REALs)

You need a polyline or a lwpolyline ENAME to use with the entget function.
With both entsel and ssget, you can retrieve the ENAME of a selected entity.

Using entsel to select a single entity is probably the easiest way, it allows to display a message and the ENAME is got with a simple 'car'. But it needs to perform a test the entity type.
Code: [Select]
(setq ent (car (entsel "\nSelect a polyline: "))) ; returns an ENAME
(setq elst (entget ent)) ; returns a 'DXF list'
(setq entType (cdr (assoc 0 elst)))
(if (member entType '("POLYLINE" "LWPOLYLINE"))
  (alert (strcat "Right type:\n" entType))
  (alert (strcat "Wrong type:\n" entType))
)

ssget can be used to select a single entity with the ":S" (or better "_:S") option and accepts a selection filter to insure the entity type selected but it's not as simple to get the ENAME form the selection set ename collection.
Code: [Select]
(setq ss (ssget "_:S" '((0 . "*POLYLINE")))) ; returns a PICKSET
(setq ent (ssname ss 0)) ; returns an ENAME : the first item (index 0) of the ENAME collection of the selection set
(setq elst (entget ent)) ; returns a 'DXF list'
(setq entType (cdr (assoc 0 elst)))
(alert (strcat "Can't be a wrong type:\n" entType)) ; always "POLYLINE" or "LWPOLYLINE" because of the filter
« Last Edit: July 04, 2010, 04:45:11 AM by gile »
Speaking English as a French Frog

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Function if with entget confusion
« Reply #6 on: July 04, 2010, 07:04:33 AM »
Did you read the AutoCAD Developer's Help for the ssget, entsel, entget functions ?

Thanks for your explanation, It is highly appreciated

In regard with what you have asked me about , I would tell you that there are so clear to me. But the problem is that when I want to use entget it won't give me the chance to select more entities to modify them within one time, as I have earlier mentioned in my first thread.

So that's why I turned to ssget to get as much as I want , Here is where the confusion has started.

And the Problem still pending so far.

Is you would help me out with it you can try my first codes and re-invoke the Lisp file to see it clearly.


Many Thanks

Tharwat

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Function if with entget confusion
« Reply #7 on: July 04, 2010, 07:50:57 AM »
If you want to deal with a selection set content, you have to loop through the selection set using ssname and an incremented index to get each ename:

Code: [Select]
(if (setq ss (ssget '((0 . "*POLYLINE"))))
  (progn
    (setq n 0) ; initialise the index
    (while (setq ent (ssname ss n)) ; loops through the selection set
      (setq n (1+ n)) ; increments the index
      ;; do your stuff for each entity here, example:
      (setq elst (entget ent))
      (princ (strcat "\nSelected a: "
     (cdr (assoc 0 elst))
     " on layer: "
     (cdr (assoc 8 elst))
     )
      )
    ) ;_ end while
  ) ;_ end progn
) ;_ end if
Speaking English as a French Frog

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Function if with entget confusion
« Reply #8 on: July 04, 2010, 08:47:37 AM »
Hi
Thanks gile.
Check this out please . this will change the color of the selected entity only. so no chances to get more than one. So according to my needs it has to be able to select multiple entities one time .
So could you change it with ssget please.
Code: [Select]
(defun c:chl (/ ent1 ent2 entNew sgOld sg1 sgNew)
(setq ent1 (car(entsel"\nSelect something:"))
      ent2 (entget ent1)
            )
(setq entNew (getint"\nSpecify New color for Layer:<1 - 255>")
      sgOld (assoc 67 ent2)
      sg1 (cons 62 entNew)
      sgNew(subst sg1 sgOld ent2)
     )
  (entmod sgNew)
  (princ)
  )
Many Thanks

Tharwat

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Function if with entget confusion
« Reply #9 on: July 04, 2010, 09:26:00 AM »
Here's an example of stepping through the selection and entmod'ing the color. It also shows a better way to specify the color change.

Code: [Select]
(defun c:Test (/ ss c)
  (if (and (setq ss (ssget "_:L"))
           (setq c (acad_colordlg 1 T))
      )
    ((lambda (i / eLst ass)
       (while (setq e (ssname ss (setq i (1+ i))))
         (if (setq ass (assoc 62 (setq eLst (entget e))))
           (entmod (subst (cons 62 c) ass eLst))
           (entmod (append eLst (list (cons 62 c))))
         )
       )
     )
      -1
    )
  )
  (princ)
)

If the object is ByLayer, it will not have the 62 code, so you have to append it to the list.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Function if with entget confusion
« Reply #10 on: July 04, 2010, 09:44:40 AM »
If you want to learn coding, you first need to avoid adding confusion to confusion.
Every time you get a reply to a question you ask another one in a different direction (here in TheSwamp as in the in the other places you posted).
Give you one single goal (not too difficult to begin) and don't change while you solve it.

I show you a way in my previous reply to access to each entity in a selection set, you only have to replace the "do your stuff for each entity here" example by what you want to do with each entity (ent).

Changing the entity color with entmod isn't the simplest task because the code group 62 is optional. so you have to test if it's present in the DXF list before substituing it if it's present,or appending it if it's not present:

Code: [Select]
(setq elst (entget ent))
(if (setq cg62 (assoc 62 elst))
  (setq elst (subst (cons 62 1) cg62 elst)) ; replace the existent code group by (62 . 1) (red)
  (setq elst (append elst (list (cons  62 1)))) ; append a 62 code group (62 . 1) to the DXF list
)
« Last Edit: July 04, 2010, 09:58:50 AM by gile »
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Function if with entget confusion
« Reply #11 on: July 04, 2010, 09:48:34 AM »
Alan,

I think the OP have to learn some AutoLISP basics before looking to the vla examples...

This kind of example should be more appropriated:
Code: [Select]
(defun c:DoIt (/ color selSet)
  (if (and (setq color (acad_colordlg 256 nil))
   (setq selSet (ssget))
      )
    (command "_.chprop" selSet "" "_color" color "")
  )
  (princ)
)
« Last Edit: July 04, 2010, 10:05:29 AM by gile »
Speaking English as a French Frog

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Function if with entget confusion
« Reply #12 on: July 04, 2010, 09:51:28 AM »
Alan,

I think the OP have to learn some AutoLISP basics before looking to the vla examples...
Excellent point Gile. Post removed.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CHulse

  • Swamp Rat
  • Posts: 504
Re: Function if with entget confusion
« Reply #13 on: July 07, 2010, 10:48:28 AM »
Did you read the AutoCAD Developer's Help for the ssget, entsel, entget functions ?

You have to always ask yourself: "what data type the function I'm using requires and what data type it returns ?"
for example:
entget requires an ENAME
ssget returns a PICKSET (a selection set is a quite complex object containing ENAMEs and informations about how it was created)
entsel returns a list which first item is an ENAME and second a point (a list of 3 REALs)

You need a polyline or a lwpolyline ENAME to use with the entget function.
With both entsel and ssget, you can retrieve the ENAME of a selected entity.

Using entsel to select a single entity is probably the easiest way, it allows to display a message and the ENAME is got with a simple 'car'. But it needs to perform a test the entity type.
Code: [Select]
(setq ent (car (entsel "\nSelect a polyline: "))) ; returns an ENAME
(setq elst (entget ent)) ; returns a 'DXF list'
(setq entType (cdr (assoc 0 elst)))
(if (member entType '("POLYLINE" "LWPOLYLINE"))
  (alert (strcat "Right type:\n" entType))
  (alert (strcat "Wrong type:\n" entType))
)

ssget can be used to select a single entity with the ":S" (or better "_:S") option and accepts a selection filter to insure the entity type selected but it's not as simple to get the ENAME form the selection set ename collection.
Code: [Select]
(setq ss (ssget "_:S" '((0 . "*POLYLINE")))) ; returns a PICKSET
(setq ent (ssname ss 0)) ; returns an ENAME : the first item (index 0) of the ENAME collection of the selection set
(setq elst (entget ent)) ; returns a 'DXF list'
(setq entType (cdr (assoc 0 elst)))
(alert (strcat "Can't be a wrong type:\n" entType)) ; always "POLYLINE" or "LWPOLYLINE" because of the filter



Thank you Gile, this was extermely helpful for me (learning the basics) :)
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023