Selection set is only using the last selected entity to compare if statement conditions

Selection set is only using the last selected entity to compare if statement conditions

Woon22
Contributor Contributor
3,284 Views
26 Replies
Message 1 of 27

Selection set is only using the last selected entity to compare if statement conditions

Woon22
Contributor
Contributor

Hello,

 

I am pretty new to lisp and have created a few programs for my work. One program I use creates holes within a rectangle by creating a boundary within an area then storing the len and wid and creating holes within. There are basically a bunch of cond statements that compare the hole distance and then creates more holes if the spacing is exceeded. I want to be able to use a selection set to select all of the rectangles at once and then apply the cond statements to each rectangle and create all the holes at once. I have the selection stored and all and the code extracts the values I need from the selection set so that I can create the pattern, however the issue I am having is that the cond. statements only apply to the last rectangle in the selection set (I believe that the selection set order is based on creation order if selected with a window). I think that every time a new len and wid of the rectangle is defined it overwrites the previous data and does not execute the conditional statements until the last entity is selected. I am wondering if anyone has any thoughts on how I could approach this problem? I know that I can create new variables within the code to match the number of entities within the selection set (I have a while loop that I am pretty sure makes sure the code is executed before the next selected entity is created). I could store a new version of len and wid for each selected entity say len1 and wid1 using read and set, but I don't know how to use the cond statement to compare a newly stored variable without literally copying and pasting all of the cond statements. 

 

For example:

 

(cond ((and (> wid1 len1)(< len 8.5)(< wid 8.5)
(entmake (list (cons 0 "CIRCLE")(cons 10 holex3y1)(cons 40 0.25)(cons 62 2)))
(entmake (list (cons 0 "CIRCLE")(cons 10 holex3y2)(cons 40 0.25)(cons 62 2)))
)
)
)

 

Is it possible to compare (widx) and (lenx) x being the current entity in the selection set that I need the program to evaluate?

Any thoughts on this would be greatly appreciated (I can post example portions of the code as well but figured I'd get the issue fleshed out in writing before that). Thanks!  

Woon22_0-1644166778494.png

 

0 Likes
Accepted solutions (1)
3,285 Views
26 Replies
Replies (26)
Message 2 of 27

devitg
Advisor
Advisor

@Woon22 

 

Hi. For better understanding, and maybe get further help, please upload such sample.dwg and LSP 

0 Likes
Message 3 of 27

Woon22
Contributor
Contributor

Hi, here is a simplified version of the code which creates 1-1 through 3-3 hole combinations (the logic doesn't change for larger combinations). I've also attached a drawing with the rectangles created in the order I need them (right to left bottom to top). If you run the lisp on the program and use a selection window over all of the rectangles it will only create holes for the first rectangle. 

0 Likes
Message 4 of 27

john.uhden
Mentor
Mentor
Accepted solution

@Woon22 

Perhaps it's because most of the action is taking place after the (while ...) loop.

And why would you change n?  It's the length of the selection set, not a counter.

You can't reenter a while loop.

John F. Uhden

Message 5 of 27

Woon22
Contributor
Contributor

Thank you so much for the insight you are absolutely correct! I was stitching another program I have together with the individual hole creation version of this lisp, and I put everything outside of the while loop... Also, the n changed for a different program for a different function, so it was an unwanted leftover. Do you have any insight on how I might be able to change my selection set so that the order is based on how the window selection covers it? For example, if I dragged my window selection from left to right and top to bottom the first ssname list I would get would be the first rectangle at the top left of the drawing? (Probably not describing this great). Either way this was very helpful and I know I would not have realized it for a good while.

0 Likes
Message 6 of 27

john.uhden
Mentor
Mentor

@Woon22 ,

Thank goodness it was just a mistake and not stupidity.

Look into the ssnamex function.  I am not at all well versed in it, but I think it can reveal the method of selection.

OR

A workround would be to use

(setq p1 (getpoint "<your prompt>: "))

  and

(setq p2 (getcorner p1 "<your prompt>: "))

  which you could sort by whatever lambda you want

in advance of an (ssget "_C" p1 p2) or (ssget "_W" p1 p2)

  to each of which you could add a filter.

But I still think you would have to sort the selection set based on their position relative to the corners picked.

Though determining the position is dubious.  It's easy for a point or a circle because they each have only one coordinate.

But for text or a block insertion do you use their insertion point (or alignment point) or say the gcen of a bounding box, or the min and max of a bounding box?  Then there are lines, arcs, and polylines of various flavors.

Hopefully ssnamex is as revealing as I might imagine.

Ya know, eventually you'll have to do something with that infantile code for determining locations.  It looks like my first attempts where I calculated every point (and X and Y therof) and stored each in a separate variable rather than treating say a polyline as a list.

Remember that Lisp got its name for being LISt Processing.

John F. Uhden

0 Likes
Message 7 of 27

Sea-Haven
Mentor
Mentor

I am confused about the rules for the holes min Length min width size is ok. I drew a big rectang and it only did holes in corners, but your image shows multiple holes for same type of shape. What is the spacing rules of the multiple holes for the length and width ? 

 

I think there will be a better method out there looking at a row & column approach. But that is a full rewrite I am sure an alternative is out there.

 

SeaHaven_0-1644193293173.png

 

0 Likes
Message 8 of 27

devitg
Advisor
Advisor

@Woon22 & @john.uhden , it does not matter how you do select the polys , like by "X" "W" pt1 pt2 , or "W" pt2 ot1, neither with "C" pt1 pt2 or "C" pt2 pt1. 

All give the same handle list , and handle follow the drawing order ; see this list , I convert hex handle to decimal 

 

;ss-by x (639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654) 
;;ss by w pt1 pt2 (639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654) 
;;ss by C pt1 pt2 (639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654)
;; ss by w pt2 pt1(639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654) 
;;ss by C pt2 pt1 (639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654)
  

devitg_0-1644193075087.png

see attached DWG 

 

0 Likes
Message 9 of 27

john.uhden
Mentor
Mentor

Hi, Gabe.

I think @Woon22 's objective was to put the objects in order of from left to right or bottom to top or both, not in their order of creation.  So, either ssnamex is the Rosetta Stone, or he must vl-sort the objects using whatever lambda suits his objective.

John F. Uhden

0 Likes
Message 10 of 27

devitg
Advisor
Advisor

@john.uhden The main problem , here  and as often, the O Posters  does not show a really and true case . Now there are wider rectangles. 

And they want to show the HOW and not the WHAT .

 Up now I do no get what the what is . 

It seem to be he want to make the 4 holes at a given X and Y from each corner , and then , and according to each wide and height, locate some equally spaced holes  depending on the distance from each "corner" holes.

Neither I understand why to get the order . 

  

 

 

 

0 Likes
Message 11 of 27

Woon22
Contributor
Contributor

Hi,

 

I revised the code so that it only does 1-1 through 3-3 possibilities for hole creation, the actual code I use for work goes up to 6-3 and 3-6 hole combinations. The rule spacing is once the hole distance exceeds 24in then it adds another hole. The code works the way I need now thanks to John's reply but I would be interested to examine other ways that I could have created this code. 

0 Likes
Message 12 of 27

Woon22
Contributor
Contributor

Untrue I am not looking to change the program to add more holes, I already know how I was just trying to figure out how to use selection set in conjunction with the current program to add holes to all rectangles at once, which John was able to help me out with. 

0 Likes
Message 13 of 27

Woon22
Contributor
Contributor
Ha well I have definitely already had some stupid moments when I am trying to write lisp. I appreciate the suggestions and will let you know if I am able to work out the issue.
0 Likes
Message 14 of 27

Sea-Haven
Mentor
Mentor

Like devitg look at it from a row column solution and its way easier I am sure I have some where make holes given a length.

 

Ok its easy look at

total len - offset*2 from edge

divide the remainder by as you suggest 24 this is a number of extra holes but as a real so need to convert to a integer

divide the remainder by count this is the spacing for the holes

do again for height so you now have a row and column how many and spacing

just do a double repeat 

all done

 

Please post a real dwg with the different hole patterns 

 

A previous example

SeaHaven_1-1644404008640.png

 

 

 

 

 

 

0 Likes
Message 15 of 27

Woon22
Contributor
Contributor

I will have to look into this method over the weekend as I am curious what other techniques I could use to create better programs. See the attached drawing with sized different parts, they all should be symmetrical unless the holes were moved to align vertically with the parts above. 

0 Likes
Message 16 of 27

Sea-Haven
Mentor
Mentor

Try this you will see the rows columns being used.

 

 

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/selection-set-is-only-using-the-last-selected-entity-to-compare/td-p/10932021

; multi holes in rectang 

; By AlanH Feb 2022 [email protected]

(defun c:recholes ( / oldsnap obj pointmin pointmax len wid ans voff hoff rad maxg dist1 dist2 rows cols vdist hdist)

(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)

(setq obj (vlax-ename->vla-object (car (entsel "pick object "))))

(vla-GetBoundingBox obj 'minpoint 'maxpoint)
(setq pointmin (vlax-safearray->list minpoint))
(setq pointmax (vlax-safearray->list maxpoint))

(setq len (- (car pointmax)(car pointmin)) wid (- (cadr pointmax)(cadr pointmin)))

(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter values" "vertical offset" 5 4 "1.5" "Horizontal offset" 5 4 "2.75" "Max space " 5 4 "24" "Radius" 5 4 "0.2188")))

(setq voff (atof (nth 0 ans))
hoff (atof (nth 1 ans))
maxg (atof (nth 2 ans))
rad (atof (nth 3 ans))
)

(setq dist1  (- len (* 2.0 hoff)) dist2 (- wid (* 2 voff)))

(setq rows (/ dist2 maxg))
(if (> rows 1.0)
(progn
(setq rows (+ (fix rows) 1))
(setq vdist (/ dist2 rows))
(setq rows (+ rows 1))
)
(progn
(setq vdist (- wid (* 2.0 voff)))
(setq rows 2)
)
)

(setq cols (/ dist1 maxg))
(if (> cols 1.0)
(progn
(setq cols (+ (fix cols) 1))
(setq hdist (/ dist1 cols))
(setq cols (+ cols 1))
)
(setq cols 1)
)

(setq pt1 (mapcar '+ pointmin (list hoff voff 0.0)))
(setq pt pt1)

(setq num 0)
(repeat rows
(setq num (+ num 1))
(repeat cols
(command "circle" pt rad)
(setq pt (mapcar '+ pt (list hdist 0.0 0.0)))
)
(setq pt (mapcar '+ pt1 (list 0.0 (* vdist num )  0.0)))
)
(setvar 'osmode oldsnap)
(princ)
)

(c:recholes)

 

 

 You must download multi getvals as well. Just pick the rectang its size will be found.

 

SeaHaven_0-1644466025059.png

 

0 Likes
Message 17 of 27

Woon22
Contributor
Contributor

Interesting, at a quick glance I can follow some of this, I do not quite understand the repeating the rows and columns. Also I still have trouble understanding mapcar/higher order lambda functions. So GETVALS allows the user to be able use multiple inputs? (veroff, horoff, ect)

0 Likes
Message 18 of 27

Woon22
Contributor
Contributor

Interesting, at a quick glance I can follow some of this, I do not quite understand the repeating the rows and columns. Also, I still have trouble understanding mapcar/higher order lambda functions. So GETVALS allows the user to be able use multiple inputs? (veroff, horoff, ect)

0 Likes
Message 19 of 27

Sea-Haven
Mentor
Mentor

Did you run it ? For me made what you wanted tested on various rectangs, horizontal and vertical.

 

Just pick one of your rectangs when asked, the dialouge opens with your default settings you can change them if required, else press OK All done !

 

Re mapcar for points its easier than doing polar to work out new pts as can add X or Y or X&Y or X&Y&Z.

 

The dwg to me has some odd drafting re the panels maybe just me.

 

I have done rotated rectangs also another day. 

0 Likes
Message 20 of 27

Woon22
Contributor
Contributor

Yes I ran it, and it works perfectly, definitely a cleaner way to do then what I did, and it can produce infinite possibilities. I am just curious to understand what it is all doing so maybe I can add a few things to my lisp knowledge. How would you go about making it possible to do multiple rectangles at once? 

0 Likes