LSP for Find an object and replace at an offset location.

LSP for Find an object and replace at an offset location.

Anonymous
Not applicable
1,462 Views
8 Replies
Message 1 of 9

LSP for Find an object and replace at an offset location.

Anonymous
Not applicable

 Hi there, 

 

I'm new to AutoLisp, and I need some help. 

 

I'm trying to write a lisp that looks for a specific block and it would replace it with another block. 

 

The issue I'm having is that the new block is being inserted in the center point of the initial block, with the insertion point of the 2nd block being the bottom left corner. 

This causes the inserted block to have an offset. Is there a way to modify the location of the inserted block by X, Y points?

Example:

If the first block's midpoint coordinate is at (10,10) i want to insert the new block at an offset of (3,-2) from (10,10), so the insertion point will be at (13,8)

 

Part 2 

Also if the script finds multiple objects (i'm looking for these objects using their layer) 

Is there a way to tell it to select the object with the greater width?

 

NOTE: I'm trying to make this script to run without any user input.

 

Your help is greatly appreciate it. 

 

Thanks

0 Likes
Accepted solutions (1)
1,463 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

....

If the first block's midpoint coordinate is at (10,10) i want to insert the new block at an offset of (3,-2) from (10,10), so the insertion point will be at (13,8)

....


(command "_.insert" "YourBlockName" (mapcar '+ YourMidPointVariable '(3 -2 0)) ....[scales, rotation, etc.]

Kent Cooper, AIA
Message 3 of 9

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

Part 2 

Also if the script finds multiple objects (i'm looking for these objects using their layer) 

Is there a way to tell it to select the object with the greater width?

....


That would require selecting all potential objects, and then stepping through them all, finding their widths, and comparing.  If they're Blocks  of the same name and 0 rotation but different X scale factors, it's pretty easy to extract the X scale factor for comparison.  If they're horizontal Lines, it's easy to get their lengths from their start and end points, or even directly with (vlax-curve...) functions.  If they're Circles, the radius, etc.  If they're certain other kinds of things, or Blocks with other variables such as different rotation angles that affect their width, you'd probably need to get their bounding boxes and calculate their widths by subtracting the X scale factor of the lower left corner from that of the upper right corner for each.  What kind(s) of objects are you talking about?

Kent Cooper, AIA
0 Likes
Message 4 of 9

Anonymous
Not applicable

Kent,

 

Thank you so much for the quick replay, this part worked perfectly. 

0 Likes
Message 5 of 9

Anonymous
Not applicable

sorry for not being specific. 

The objects are rectangles with text inside.

The layer i'm finding these objects in, should not have more than 2 of these objects.

 

I was thinking if there was more than 3 objects, then i would get the user to select which block to replace. 

but if there was 2 objects, then I was planning on trying to get the script to figure out which object to replace based on width. These objects have similar heights, but different width due to the length of the text inside the object. (the rectangles inside the objects are not adjustable, unless I explode the object, but i don't want to do that). 

 

0 Likes
Message 6 of 9

oscardosanjos
Enthusiast
Enthusiast

Hi,

 

"This causes the inserted block to have an offset. Is there a way to modify the location of the inserted block by X, Y points?

If the first block's midpoint coordinate is at (10,10) i want to insert the new block at an offset of (3,-2) from (10,10), so the insertion point will be at (13,8)"

 

Yes,there is.

first you have to collect those coordinates that you want to modify and keep them in some variables, then you have to can rewrite them, example base on yours:

 

(setq x_coord 10)

(setq y_coord 10)

 

(setq x_coord (+ x_coord 3 ))

(setq y_coord (- y_coord 2 ))

 

Now use the variables already created inserting your new blocks.

 

 

 

 

 

 

0 Likes
Message 7 of 9

DannyNL
Advisor
Advisor

If it's just a rectangle with text and you are interested in the width to be able to determine which block to replace, I'm assuming you want to replace the original with a block with a larger rectangle so it will fit the larger text?

 

Why not create a new dynamic block with a sizable rectangle and attribute? This way you will only need one block that will have a default width and can be changed if the text becomes to large to fit in. The changing of the width could even be automated with the use of LISP if necessary.

0 Likes
Message 8 of 9

Anonymous
Not applicable

Thanks Danny for the idea. 

However, I'm working with a library that I'm not suppose to modify any of its components. 

That's why I was faced with the 1st issue, where the selection point of one object was the midpoint of the block, and the selection point of the 2nd object was bottom left, which caused the offset when I was using the LISP to replace the object. 

 

 

0 Likes
Message 9 of 9

Anonymous
Not applicable

Thanks Danny for the idea.

However, I'm working with a library that I'm not allowed to add or modify any of its components/objects.

This is why I was faced with the 1st issue where the selection point of one object was the middle point and the selection point of the other object was the bottom left. Which cause the offset when replacing objects with the LISP.  

 

0 Likes