PDA

View Full Version : Test for correct type of object


Glen_Johnston
2007-07-01, 02:07 AM
Hi,

I design truck bodies with compartments of various sizes. The compartment is drawn using a rectangle. I have written a lisp program that extracts the width and height data from the rectangle, and then it extracts the compartment location, number, and hinge type from a text object located within the rectangle. After that has been done, the program generates a cut list for the aluminum extrusions that make up both the door jamb and door frame. The program works great, but I would like to improve one aspect of it.

The program fails if the person using it (mostly me) picks empty space for either the rectangle or text, or miss-picks the wrong type of object for either the rectangle or text. I would like to add a “loop-back” (Please try again!...) until the correct object has been picked at the proper time.

I did a search and did find some threads that showed a few different methods to do this, but I just could not figure out how to include that code into the selection portion of my program (shown below)

One method found used “errno” 7 if there was a pick in empty space, and another method tested the selection to see if it was the correct type of object and used a “while” loop. As I said, I just cannot figure out how to do it. Any help would be appreciated. If the entire program is needed, let me know.

Thanks, Glen

This program will be used on AutoCAD 2005, 2006, & 2007



; Get raw compartment dimensions

(setq rect (entget (car(entsel "SELECT COMPARTMENT"))))
(print)
(setq corner1 (nth 14 rect))
(setq corner2 (nth 22 rect))
(setq high (- (caddr corner1) (caddr corner2)))
(setq wide (- (cadr corner1) (cadr corner2)))
(setq rawheight (abs high))
(setq rawwidth (abs wide))
(setq entSelection (entsel "SELECT COMPARTMENT NUMBER: ")) ;select Compartment NAME/NUMBER/HINGE STYLE - Example: CS-1-S
(print)
(setq entstring (cdr (assoc 1 (entget (car entSelection))))) ;extract text value
(setq answer (substr entstring 6)) ;extract hinge style from text value
(setq entstring (substr entstring 1 4)) ;reduce text value to 4 places

fixo
2007-07-01, 08:46 AM
Try to use filter for selection set,
something like:


(setq entSelection (ssget (list (cons 0 "TEXT"))));select texts only
(setq enttext (ssname entSelection 0)); get first text entity in selection
(setq entlist (entget enttext)); get entity lis
(setq entstring (cdr (assoc 1 entlist)));extract text value
(setq answer (substr entstring 6));extract hinge style from text value
(setq entstring (substr entstring 1 4));reduce text value to 4 places


~'J'~

Glen_Johnston
2007-07-01, 01:51 PM
Thanks for that!

I'll check it out in the program later today after the grandkids go home. I'll let you know

Glen

fixo
2007-07-01, 03:35 PM
Don't hurry up, better yet to play with grandkids
than with lisp :)
Also, you can try another one, I don't know
who is an author of this thing:

(defun pick (msg / flag)
(if (not msg)
(setq msg "\nSelect object: ")
)
(while (not flag)
(setvar "errno" 0)
(if (or (setq na (entsel msg))
(= (getvar "errno") 52)
)
(progn
(setq flag T)
(setq pt (cadr na))
(setq na (car na))
)
(progn
(if (/= (getvar "errno") 52)
(alert "you missed! Try Again!")
)
)
)
)
na
)

In main part you can call it with brackets:

(defun c:main (/ )
............
(pick)
<<then working further with variable "na"
to check on correct entity type as you
wrote in prior post>>
(if (eq "TEXT (cdr (assoc 0 (entget na)))
............

Let me know if you need something else

~'J'~

Glen_Johnston
2007-07-02, 12:32 PM
OK, I got a chance to try selection set filter you suggested, and it does work. Is there a way to make it exit the selection loop once you have selected a text object (it asks to select more objects, I only want to select 1 text object for that phase of the program)?

I worked on the other method you suggested, but did not have enough time to get it working within my program, I'll let you know later what the results are.

Thanks, Glen

ccowgill
2007-07-02, 01:51 PM
OK, I got a chance to try selection set filter you suggested, and it does work. Is there a way to make it exit the selection loop once you have selected a text object (it asks to select more objects, I only want to select 1 text object for that phase of the program)?

I worked on the other method you suggested, but did not have enough time to get it working within my program, I'll let you know later what the results are.

Thanks, Glen
take a look at ssget in the developer help, it explains what each sel-method does and how to only allow selection of one item.

fixo
2007-07-02, 02:11 PM
OK, I got a chance to try selection set filter you suggested, and it does work. Is there a way to make it exit the selection loop once you have selected a text object (it asks to select more objects, I only want to select 1 text object for that phase of the program)?

I worked on the other method you suggested, but did not have enough time to get it working within my program, I'll let you know later what the results are.

Thanks, Glen


Hi Glenn,
Here is another way, that allows you to select the single text entity
You can miss many times till you select them, the old trick though
Just replace the following snip with the first line of code above

(setq entSelection nil); clear selection set if this does used before, optional
(while (not (setq entSelection (ssget "+.:S:E" (list (cons 0 "TEXT"))))))
; where + to combine selection modes, S - forces single selection mode, E- forces
; selection mode entire the cursor box size, N - nested entity selection mode etc.
;You can use complex selection filter like this
(ssget "+.:S:E" (list (cons 0 "TEXT")(cons 8 "Layer1, Layer2, Layer3")(cons 40 0.2)))
;to select single text entity on one of these layers with text height 0.2
; Test:
(alert (strcat "Selected: " (itoa (sslength entSelection)) " text(s)"))

Sorry for my dim explanation, hope you'll understand me
And I agreed with ccowgill, take a look at Help :)

~'J'~

jwanstaett
2007-07-02, 09:52 PM
try this function
it return the entity name of the entity it will not exit till you pick one of the type you ask for.

;;test Function to test selectone
;;Will Select one line
(defun c:Testme ()
(entget (selectone "LINE"))
)

;;Function Selectone( myentitytype)
;; input myEntityType
;; the name of the type of entity to select
;; Exp: "LINE" "TEXT"
;; NOTE CASE NEED TO MATCH
;; Return
;; The name of the entity Selected
(defun SelectOne (myEntityType / myflage mytype)
(setq myFlage nil)
(while (= myflage nil)
(if (setq myflage (entsel (strcat " Select A " myEntityType )))
(progn
(setq myflage (car myflage))
(setq mytype (cdr (assoc 0 (entget myflage))))
(if (/= mytype myentitytype)
(progn
(setq myflage nil)
(prompt
(strcat "nEnity Type " myentitytype " Not Selected"))
)
)

)
)


)
(setq myflage myflage)
)

Glen_Johnston
2007-07-02, 10:28 PM
I've had a pretty busy day, so I have not had a chance to test any of the great solutions yet! Hopefully tomorrow I'll have enough time to give it a go. I will post the results.

Thanks to all!