TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jaydee on September 20, 2011, 01:53:48 AM
-
How to provide a while loop to continue entsel, if missed, try again.
at the same time allow to continue to select until right click to exit (what im only manage is Esc to exit)
(defun c:test (/ en)
;;;how to provide another
;(while here
(while
(not (setq en (car (entsel))))
(prompt "\n Missed, try again")
)
(command "_chprop" en "" "c" "1" "")
;)
(princ)
)
-
I don't think right-click (since you can only get that using grread, which then means entsel doesn't happen). Unfortunately only space/enter would stop entsel without erroring out like esc does. So you could go and use initget with a keyword and 128 as the code. Then also add that keyword as a default (i.e. in between <> in the message). If you do so, then entsel would return the keyword string when the user pressed Enter/Space.
As a sample:(defun EntSelChk (msg init chk / en err)
(while (and (progn
(if init
(apply 'initget init)
)
(setq en (entsel msg))
)
(listp en)
(setq err (apply chk (list en)))
)
(princ err)
)
en
)
(defun c:Test (/ en check)
(defun check (en / )
(if (not (wcmatch (cdr (assoc 0 (entget (car en)))) "LINE,LWPOLYLINE"))
"\nThat's not a line / polyline, please try again."
)
)
(while (and (setq en (EntSelChk "\nSelect line / polyline <Exit>: " '("Exit" 128) 'check))
(not (eq en "Exit")))
(princ "\nYou've picked an acceptable entity")
)
(princ "\nYou've selected to exit.")
(princ)
)
Edit: scratch that, just realised it still doesn't do as intended. Will look a bit further and come back.
-
No Right Click exit but an old subroutine of mine that allows Enter Exit.
(defun getEntity (msg enttype / picked ent return)
;; enttype may be nil
(setvar "ErrNo" 0) ; reset variable
(while
(not
(cond
((and (null (setq picked (entsel msg)))
(/= 52 (getvar "ErrNo")) ; <Enter> was not hit
)
(prompt "\nMissed, Try Again.")
)
((null picked) t) ; [ENTER] pressed, exit loop, return nil
((progn
(setq ent (car picked))
(cond
((null enttype) (setq return ent))
((= (strcase enttype) (cdr (assoc 0 (entget ent))))
(setq return ent)
)
(t (prompt "\nWrong entity type, Try Again."))
)
)
)
)
)
)
return
)
A more elaborate routine can be found here:
http://www.theswamp.org/index.php?topic=21655.msg262245#msg262245
-
I have always liked (ssget) for these kind of selections. It's built in. -David
-
do you want something like this ?
(defun c:TEST (/ OB DT)
(while
(progn
(initget "Exit ")
(if (setq OB (entsel "\nSelect line [Exit]: "))
(cond
( (or (= OB "Exit") (= OB ""))
(princ "\n>> Exit TEST routine. <<")
nil
)
( (/= (setq DT (cdr (assoc 0 (entget (car OB))))) "LINE")
(princ "\n** Wrong object selected **")
T
)
( (= DT "LINE")
(princ "\n>> You select a line. <<")
nil
)
)
(progn
(princ "\n** Nothing selected **")
T
)
)
)
)
(princ)
)
k.
-
My offering...
http://lee-mac.com/selectif.html (http://lee-mac.com/selectif.html)
-
Probably, most approximately to what you wanted :
(only one while loop is needed)
(defun c:test (/ en)
(while (or (setq en (car (entsel))) (listp (cadr (grread nil 2))) )
(if (not en)
(prompt "\n Missed, try again")
(command "_chprop" en "" "c" "1" "")
)
)
(princ)
)
M.R.
-
And this is even worse, but more logical :
(defun or2 (exp1 exp2)
(if (and (eq exp1 T) (eq exp2 T)) T
(if (and (eq exp1 T) (eq exp2 nil)) T
(if (and (eq exp1 nil) (eq exp2 T)) nil
(if (and (eq exp1 nil) (eq exp2 nil)) nil
))))
)
(defun c:test (/ en)
(while (or2 (listp (cadr (grread nil 2))) (if (setq en (car (entsel))) T) )
(if (not en)
(prompt "\n Missed, try again")
(command "_chprop" en "" "c" "1" "")
)
)
(princ)
)
M.R.
-
Marko,
Isn't this:
(defun or2 (exp1 exp2)
(if (and (eq exp1 T) (eq exp2 T)) T
(if (and (eq exp1 T) (eq exp2 nil)) T
(if (and (eq exp1 nil) (eq exp2 T)) nil
(if (and (eq exp1 nil) (eq exp2 nil)) nil
))))
)
Just:
(defun or2 ( exp1 exp2 ) (and exp1))
Since the expression returns T irrelevant of the value of exp2.
-
Marko,
Isn't this:
(defun or2 (exp1 exp2)
(if (and (eq exp1 T) (eq exp2 T)) T
(if (and (eq exp1 T) (eq exp2 nil)) T
(if (and (eq exp1 nil) (eq exp2 T)) nil
(if (and (eq exp1 nil) (eq exp2 nil)) nil
))))
)
Just:
(defun or2 ( exp1 exp2 ) (and exp1))
Since the expression returns T irrelevant of the value of exp2.
I was about to ask the same thing.
-
Yes, you're probably wright... Now I swapped exp1 and exp2 in hope to get some different results, but no success... :|
(defun or2 (exp1 exp2)
(if (and (eq exp1 T) (eq exp2 T)) T
(if (and (eq exp1 T) (eq exp2 nil)) nil
(if (and (eq exp1 nil) (eq exp2 T)) T
(if (and (eq exp1 nil) (eq exp2 nil)) nil
))))
)
(defun c:test (/ en)
(while (or2 (if (setq en (car (entsel))) T) (listp (cadr (grread nil 2))) )
(if (not en)
(prompt "\n Missed, try again")
(command "_chprop" en "" "c" "1" "")
)
)
(princ)
)
M.R.
-
I'm not exactly sure what you are trying to accomplish Marko? GrRead will wait for any user input, so the user would be prompted by entsel, then the program would pause until the user provided another input.
I believe the OP's request is satisfied by CAB's, Kruuger's or my example...
-
Thankyou all for your help.
I need to test it in actual function (chprop just a simple example) to see which one suit me best.
the code provide by Lee seems promising with minor mod, Lee i hope you dont mind i repost the mod version here.
This code will allow continuous to pick entity until right click to exit, also handle missed pick.
(defun c:test1 ( / entity )
(while
(if
(setq entity
(car
(LM:SelectIf "\nSelect a Line: "
(lambda ( x ) (eq "LINE" (cdr (assoc 0 (entget (car x)))))) entsel nil
)
)
)
(progn
(command "_chprop" entity "" "c" "1" "")
(princ)
; (princ (strcat "\nHandle of Selected Line: " (cdr (assoc 5 (entget entity)))))
)
)
)
(princ)
)
-
Here is my final result, and it's working fine :
(defun c:test (/ pt ss en)
(while (listp (setq pt (cadr (grread nil (+ 2 4) 2))))
(setq ss (ssget pt))
(if ss
(progn
(setq en (ssname ss 0))
(command "_chprop" en "" "c" "1" "")
)
(prompt "\n Missed, try again")
)
)
(princ)
)
M.R.
-
Here is my final result, and it's working fine :
(defun c:test (/ pt ss en)
(while (listp (setq pt (cadr (grread nil (+ 2 4) 2))))
(setq ss (ssget pt))
(if ss
(progn
(setq en (ssname ss 0))
(command "_chprop" en "" "c" "1" "")
)
(prompt "\n Missed, try again")
)
)
(princ)
)
M.R.
So strange, it's working fine if I type TEST and run command. But, if i used up arrow to call it again from history command, it done nothing..
-
Thankyou Ribarm. Im impressed, works a treat.
Just wondering, Could it be implemented with NENTSEL
I never come across (ssget pt)
Another unrelated topic Could i apply (ssget ListofPoints)??
Cheers
-
I don't quite know for nentsel - as you see I avoided even entsel... As for ssget there are plenty info about it :
Selection Sets (http://www.afralisp.net/autolisp/tutorials/selection-sets.php)
Selection Set Filters (http://www.afralisp.net/autolisp/tutorials/selection-set-filters.php)
ssget - taken from developer guide
Creates a selection set from the selected object
(ssget [sel-method] [pt1 [pt2]] [pt-list] [filter-list])
Selection sets can contain objects from both paper and model space, but when the selection set is used in an operation, ssget filters out objects from the space not currently in effect. Selection sets returned by ssget contain main entities only (no attributes or polyline vertices).
Arguments
sel-method
A string that specifies the object selection method. Valid selection methods are
C Crossing selection.
CP Cpolygon selection (all objects crossing and inside of the specified polygon).
F Fence selection.
I Implied selection (objects selected while PICKFIRST is in effect).
L Last visible object added to the database.
P Last selection set created.
W Window selection.
WP WPolygon (all objects within the specified polygon).
X Entire database. If you specify the X selection method and do not provide a filter-list, ssget selects all entities in the database, including entities on layers that are off, frozen, and out of the visible screen.
:E Everything within the cursor's object selection pickbox.
стр. 109 из 262
AutoCAD. Visual LISP, AutoLISP и DXF. AutoLISP Reference
:N Call ssnamex for additional information on container blocks and transformation matrices for any entities selected during the ssget operation. This additional information is available only for entities selected via graphical selection methods such as Window, Crossing, and point picks.
Unlike the other object selection methods, :N may return multiple entities with the same entity name in the selection set. For example, if the user selects a subentity of a complex entity such as a BlockReference, PolygonMesh, or old style polyline, ssget looks at the subentity that is selected when determining if it has already been selected. However, ssget actually adds the main entity (BlockReference, PolygonMesh, an so on) to the selection set. The result could be multiple entries with the same entity name in the selection set (each will have different subentity information for ssnamex to report).
:S Allow single selection only.
pt1
A point relative to the selection.
pt2
A point relative to the selection.
pt-list
A list of points.
filter-list
An association list that specifies object properties. Objects that match the filter-list are added to the selection set.
If you omit all arguments, ssget prompts the user with the Select objects prompt, allowing interactive construction of a selection set.
If you supply a point but do not specify an object selection method, AutoCAD assumes the user is selecting an object by picking a single point.
Return Values
The name of the created selection set if successful; otherwise nil if no objects were selected.
Notes on the Object Selection Methods
• When using the :N selection method, if the user selects a subentity of a complex entity such as a BlockReference, PolygonMesh, or old style polyline, ssget looks at the subentity that is selected when determining if it has already been selected. However, ssget actually adds the main entity (BlockReference, PolygonMesh, etc.) to the selection set. It is therefore possible to have multiple entries with the same entity name in the selection set (each will have different subentity information for ssnamex to report). Because the :N method does not guarantee that each entry will be unique, code that relies on uniqueness should not use selection sets created using this option.
• When using the L selection method in an MDI environment, you cannot always count on the last object drawn to remain visible. For example, if your application draws a line, and the user subsequently minimizes or cascades the AutoCAD drawing window, the line may no longer be visible. If this occurs, ssget with the "L" option will return nil.
Examples
Prompt the user to select the objects to be placed in a selection set:
Command: (ssget)
<Selection set: 2>
Create a selection set of the object passing through (2,2):
Command: (ssget '(2 2))
nil
Create a selection set of the most recently selected objects:
Command: (ssget "_P")
<Selection set: 4>
Create a selection set of the objects crossing the box from (0,0) to (1,1): стр. 110 из 262
AutoCAD. Visual LISP, AutoLISP и DXF. AutoLISP Reference
Command: (ssget "_C" '(0 0) '(1 1))
<Selection set: b>
Create a selection set of the objects inside the window from (0,0):
Command: (ssget "_W" '(0 0) '(5 5))
<Selection set: d>
By specifying filters, you can obtain a selection set that includes all objects of a given type, on a given layer, or of a given color. The following example returns a selection set that consists only of blue lines that are part of the implied selection set (those objects selected while PICKFIRST is in effect):
Command: (ssget "_I" '((0 . "LINE") (62 . 5)))
<Selection set: 4>
The following examples of ssget require that a list of points be passed to the function. The pt_list variable cannot contain points that define zero-length segments.
Create a list of points:
Command: (setq pt_list '((1 1)(3 1)(5 2)(2 4)))
((1 1) (3 1) (5 2) (2 4))
Create a selection set of all objects crossing and inside the polygon defined by pt_list:
Command: (ssget "_CP" pt_list)
<Selection set: 13>
Create a selection set of all blue lines inside the polygon defined by pt_list:
Command: (ssget "_WP" pt_list '((0 . "LINE") (62 . 5)))
<Selection set: 8>
The selected objects are highlighted only when ssget is used with no arguments. Selection sets consume AutoCAD temporary file slots, so AutoLISP is not permitted to have more than 128 open at one time. If this limit is reached, AutoCAD refuses to create any more selection sets and returns nil to all ssget calls. To close an unnecessary selection set variable, set it to nil.
A selection set variable can be passed to AutoCAD in response to any Select objects prompt at which selection by Last is valid. AutoCAD then selects all the objects in the selection set variable.
The current setting of Object Snap mode is ignored by ssget unless you specifically request it while you are in the function.
------------------------------------------------------------------------------------------
SSGET - COMPOSED BY LEE MAC
------------------------------------------------------------------------------------------
(ssget [sel-method] [pt1 [pt2]] [pt-list] [filter-list])
------------------------------------------------------------------------------------------
(ssget '(2 2)) Create a selection set of the object passing through (2,2)
Point is expressed in UCS.
+. Point Selection Mode
The undocumented "+." mode forces (ssget) to remain in "point" mode,
similar to setting PickAuto to 0.
It helps the ":S" single-selection mode and ":E" mode act just like (entsel) by avoiding
implied selection windows.
------------------------------------------------------------------------------------------
A All
Like "X" but filters frozen out. Selects all objects on thawed layers.
------------------------------------------------------------------------------------------
B Box
Selects all objects inside or crossing a rectangle specified by two points. If
the rectangle's points are specified from right to left, Box is equivalent to
Crossing. Otherwise, Box is equivalent to Window.
------------------------------------------------------------------------------------------
C Crossing
Selects objects within and crossing an area defined by two points. A crossing
selection is displayed as dashed or otherwise highlighted to differentiate it
from window selection.
(ssget "_C" '(0 0) '(1 1))
Points in UCS
** Caution: the selection area must be visible on screen for this to work properly **
(command "._zoom" "_E")
------------------------------------------------------------------------------------------
CP Crossing Polygon
Selects objects within and crossing a polygon defined by specified points. The
polygon can be any shape but cannot cross or touch itself. AutoCAD draws the
last segment of the polygon so that it is closed at all times. CPolygon is not
affected by the PICKADD system variable.
(ssget "_CP" '((1 1) (3 1) (5 2) (2 4)))
Points in UCS
Example with filters:
(ssget "_CP" '<Point list> '<Filter List>)
(ssget "_CP" '((0 0) (10 0) (10 10) (0 10)) '((0 . "INSERT") (66 . 1)))
** Caution: the selection area must be visible on screen for this to work properly **
(command "._zoom" "_E")
------------------------------------------------------------------------------------------
:D Duplicates OK, else duplicates are ignored
------------------------------------------------------------------------------------------
:E Everything in aperture
Everything within the cursor's object selection pickbox.
------------------------------------------------------------------------------------------
F Fence
Selects all objects crossing a selection fence. The Fence method is similar to
CPolygon except that AutoCAD does not close the fence, and a fence can cross
itself. Fence is not affected by the PICKADD system variable.
------------------------------------------------------------------------------------------
G Groups
Selects all objects within a specified group.
------------------------------------------------------------------------------------------
I Implied
Implied selection (objects selected while PICKFIRST is in effect).
------------------------------------------------------------------------------------------
L Last
Last visible object added to the database
------------------------------------------------------------------------------------------
:L Rejects locked layers
------------------------------------------------------------------------------------------
M Multiple
Specifies multiple points without highlighting the objects, thus speeding up
the selection process for complex objects. The Multiple method also selects two
intersecting objects if the intersection point is specified twice.
------------------------------------------------------------------------------------------
:N Nested
Call ssnamex for additional information on container blocks and transformation
matrices for any entities selected during the ssget operation. This additional
information is available only for entities selected via graphical selection
methods such as Window, Crossing, and point picks.
Unlike the other object selection methods, :N may return multiple entities with
the same entity name in the selection set. For example, if the user selects a
subentity of a complex entity such as a BlockReference, PolygonMesh, or old
style polyline, ssget looks at the subentity that is selected when determining
if it has already been selected. However, ssget actually adds the main entity
(BlockReference, PolygonMesh, etc.) to the selection set. The result could be
multiple entries with the same entity name in the selection set (each will have
different subentity information for ssnamex to report).
------------------------------------------------------------------------------------------
P Previous
Selects the most recent selection set. The Previous selection set is cleared by
operations that delete objects from the drawing. AutoCAD keeps track of whether
each selection set was specified in model space or paper space. The Previous
selection set is ignored if you switch spaces.
------------------------------------------------------------------------------------------
:P Rejects Viewport
------------------------------------------------------------------------------------------
:R Allows entities in a long transaction to be selected.
------------------------------------------------------------------------------------------
:S Force single object selection only
------------------------------------------------------------------------------------------
:U Enables subentity selection - 2006+
Cannot be combined with the duplicate (":D") or nested (":N") selection modes.
In this mode, top-level entities are selected by default, but the user can
attempt to select subentities by pressing the CTRL key while making the
selection. This option is supported only with interactive selections, such as
window, crossing, and polygon. It is not supported for all, filtered, or group
selections.
------------------------------------------------------------------------------------------
:V Forces subentity selection - 2006+
Treats all interactive, graphic selections performed by the user as subentity
selections. The returned selection set contains subentities only. This option
cannot be combined with the duplicate (":D") or nested (":N") selection modes.
This option is supported only with interactive selections, such as window and
crossing. It is not supported for all, filtered, or group selections.
------------------------------------------------------------------------------------------
W Window
Selects all objects completely inside a rectangle defined by two points.
Specifying the corners from left to right creates a window selection.
(Specifying the corners from right to left creates a crossing selection.)
------------------------------------------------------------------------------------------
WP Window Polygon
Selects objects completely inside a polygon defined by points. The polygon can
be any shape but cannot cross or touch itself. AutoCAD draws the last segment of
the polygon so that it is closed at all times. WPolygon is not affected by the
PICKADD system variable.
------------------------------------------------------------------------------------------
X Extended search (search whole database)
Entire database. If you specify the X selection method and do not provide a
filter-list, ssget selects all entities in the database, including entities on
layers that are off, frozen, and out of the visible screen.
------------------------------------------------------------------------------------------
System Variables
------------------------------------------------------------------------------------------
PICKADD
Controls whether subsequent selections replace the current selection set or add to it.
0 Turns off PICKADD.
The objects and subobjects most recently selected become the selection set.
Previously selected objects and subobjects are removed from the selection set.
Add more objects or subobjects to the selection set by pressing SHIFT while selecting.
1 Turns on PICKADD. Each object and subobject selected, either individually or by windowing,
is added to the current selection set. To remove objects or subobjects from the set,
press SHIFT while selecting.
------------------------------------------------------------------------------------------
PICKAUTO
Controls automatic windowing at the Select Objects prompt.
0 Turns off PICKAUTO
1 Draws a selection window (for either a window or a crossing selection) automatically at
the Select Objects prompt
------------------------------------------------------------------------------------------
PICKDRAG
Controls the method of drawing a selection window.
0 Draws the selection window using two points.
Click the pointing device at one corner, and then click to select another corner.
1 Draws the selection window using dragging.
Click one corner and drag the pointing device, release the button at the other corner.
------------------------------------------------------------------------------------------
PICKFIRST
Controls whether you select objects before (noun-verb selection) or after you issue a command.
0 Turns off PICKFIRST, you select objects after you issue a command
1 Turns on PICKFIRST, you select objects before you issue a command
------------------------------------------------------------------------------------------
PICKSTYLE
Controls the use of group selection and associative hatch selection.
0 No group selection or associative hatch selection
1 Group selection
2 Associative hatch selection
3 Group selection and associative hatch selection
------------------------------------------------------------------------------------------
Hope this will clarify some things...
M.R. :-)
-
Hi guys,
and thank you for this inspiring post .. I really needed this one.
Just wondering, Could it be implemented with NENTSEL
This is what I came up with
(defun C:TEST (/ sel_ob e e1)
(defun sel_ob ()
(setvar 'errno 0)
(cond ((nentsel))
((= (getvar 'errno) 7) (princ "\nMissed! Try again...") (sel_ob))
(nil)
)
)
(while (setq e (sel_ob))
(princ
(strcat "\nYou've picked a "
(cdr (assoc 0 (entget (car e))))
". The object "
(cond ((eq 'ENAME (type (setq e1 (caar (reverse e)))))
(strcat "is a subEntity of a " (cdr (assoc 0 (entget e1))))
)
(T "has no subEntities")
)
)
)
)
(princ)
)
-
Thankyou Stefan for the code, Im loving it.
Also thanyou rebarm for such an extensive (ssget) list.
Could someone point me to the right direction where i could see the complete listing of the (ssget) operators.
The one i come across often are (-4 . "<or"), (-4 . "<and"), (-4 . "<not").
What else are there ?
(-4 . "???") ?
-
In the Visual LISP IDE Help Documentation, have a read of the following sections:
+ AutoLISP Developer's Guide
|
+--+ Using the AutoLISP Language
|
+--+ Using AutoLISP to Manipulate AutoCAD Objects
|
+--+ Selection Set Handling
|
+--+ Selection Set Filter Lists
|
+--> Relational Tests
|
+--> Logical Grouping of Filter Tests
-
In the Visual LISP IDE Help Documentation, have a read of the following sections:
+ AutoLISP Developer's Guide
|
+--+ Using the AutoLISP Language
|
+--+ Using AutoLISP to Manipulate AutoCAD Objects
|
+--+ Selection Set Handling
|
+--+ Selection Set Filter Lists
|
+--> Relational Tests
|
+--> Logical Grouping of Filter Tests
Just a heads up, this only works prior to AutoCAD 2011, then they switched to the online help and it's gone.
-
Just a heads up, this only works prior to AutoCAD 2011, then they switched to the online help and it's gone.
*cough*
AutoCAD 2012:
(http://i54.tinypic.com/117rajd.jpg)
-
Thankyou guys for the advice.
I admit im a bit short sighted that i never look into the "developers's guide" area, the only place i look is the "autolisp reference" section.
Cheers
-
Glad to help. There's lotsa info available these days -- not all of it is at the swamp. :-D