Need help with vl-position

Need help with vl-position

Anonymous
Not applicable
1,088 Views
3 Replies
Message 1 of 4

Need help with vl-position

Anonymous
Not applicable

ERRRRGH.  I need help using vl-position.  I've attached a dwg and a LISP file;  the dwg has 2 columns of horizontal lines.  The left column is a block, showing the result of a different "upstream" LISP routine.  I need all the lines to end like the single lines on the right. 

 

The block that these lines go in will always be in this orientation, with these lines always in the same place.  I can't figure out any other way to change the color/layer assignment of the lines that are indicated by the orange lines....so I'm trying to select/assign them by their vertical coordinates.

 

I don't want to mess with the upstream LISP routine.  These lines come from another block with a lot more going on.

 

The attached LISP file is as far as I can get, I can't get the syntax with the vl-position cooperating; when I try running the LISP, it freezes with the block editor open.

 

I started with the 23rd line down from the top.  If someone can help me get that 23rd line to go to the "HILMOT-MDR" layer, I should be able to handle the other lines myself.  Thanks in advance for any help!

0 Likes
Accepted solutions (1)
1,089 Views
3 Replies
Replies (3)
Message 2 of 4

Ranjit_Singh
Advisor
Advisor
Accepted solution

Try spending some more time on cond vs if statements. You are using unnecessary cond statements. Replace this portion of code.

 

(cond
		((= 23 (vl-position x hl))
		 (addprop (x) "HILMOT-MDR")
		)
		;;23rd line down goes to this layer
			(t (nil)
		)
	      )

with

(if (= 23 (vl-position x hl)) (addprop (last x) "HILMOT-MDR"))

 Also, if you want to change the 23rd line from top as you indicate in your comment, you should be searching for 22 and not 23. vl-position of first element is index 0, not 1.

Message 3 of 4

john.uhden
Mentor
Mentor

I think this is what you are calling your problem area...

 

	    (foreach x hl
	      (cond
		((= 23 (vl-position x hl))
		 (addprop (x) "HILMOT-MDR")
		)
		;;23rd line down goes to this layer
			(t (nil)
		)
	      )
	    )

How about just using:

 

(nth 23 hl)
?

John F. Uhden

Message 4 of 4

martti.halminen
Collaborator
Collaborator

Apart from the zero-indexing confusion the actual call to vl-position is correct. Your problem  - which the other suggestions bypass - lies elsewhere.

 

In this piece there are two errors:

 

(cond
((= 23 (vl-position x hl))
(addprop (x) "HILMOT-MDR")
)
;;23rd line down goes to this layer
(t (nil)
)
)

At this stage your X has a value which is a list of two numbers and a vla-object.
Putting parentheses around that means you are calling it as a function, which doesn't work:

_$ (setq x (list 3.5 2 nil))
(3.5 2 nil)
_$ (x)
; error: bad function: (3.5 2 nil)

- in this case you needed the vla-object, so use (last x) instead of (x)

And the same problem in your T branch of the cond:
_$ (nil)
; error: no function definition: nil

- here, (T nil) instead of (T (nil)) would cause no error. Just omitting the T branch would also work,
but it is often clearer to use an explicit T branch to show that you have thought about that case, too.

--


0 Likes