Lisp to insert block at specific location and layer

Lisp to insert block at specific location and layer

Anonymous
Not applicable
7,665 Views
40 Replies
Message 1 of 41

Lisp to insert block at specific location and layer

Anonymous
Not applicable

I am interested in a lisp that will insert a block "4x6" (Which requires no scaling) and place it at both ends of a selected line. offset 2" from that line and give the option of which side of that line they will be placed.  finally the inserted "4x6" will be placed on the "s-plan-rcol" layer.   I've included an image for disired result. No scaling or annotations or anything, just would like to show posts at the end of shear walls without having to place them manually.

 

thanks for any help

0 Likes
Accepted solutions (1)
7,666 Views
40 Replies
Replies (40)
Message 21 of 41

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

ok, 

on a line drawn from left to right; it only places the posts under the line. Regardless of what side of the line I pick.

on a line drawn from right to left it isn't rotating the posts. 

.... 

when it comest to a vertical line they aren't not being rotated and placed. 


I'm also curious about its apparently Inserting more than one Block at each end of some Lines -- is that just from trying more than once on the same Line?

 

Are you picking the to-which-side point far enough away from the Line that no running Object-Snap mode(s) can catch the Line and use some point on it?  Try it with Osnap off and see whether it still has the problems.

 

And are you picking the to-which-side point decidedly to one side of the Line, not straight off one end?  Picking directly aligned off an end could explain the ones that are aligned with the Line rather than to one side.

Kent Cooper, AIA
0 Likes
Message 22 of 41

Anonymous
Not applicable

I am absolutly picking far off of the line.  

 

see attached for updated image of what's its doing for me..

 

thanks

0 Likes
Message 23 of 41

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

see attached for updated image of what's its doing for me..

....


I can't tell at the scale of the image, but in the ones where the Blocks are aligned with the Line, are the nearest midpoints of the post boundaries of the ones that the Line does not go through 1/4" off the end of the Line?  If so, that's the difference between the half-a-post-width inset from the ends for the insertion points and the 2"-to-the-side offset, so it seems the insertion points are correct.  And since, in those cases, there's one off an end and one that the Line goes through, it seems rotation is the same for both Blocks.  That sameness of rotation is also true of the horizontal-Line situation in which it always puts them underneath.  So the problem seems to be in only its determination of the rotation angle.  I'll be back if I can think of any reason for that, but it's puzzling....  I wondered about the ANGBASE and/or ANGDIR System Variables not being at the default 0-to-the-east and counterclockwise settings, but if either were off, you'd presumably be able to at least still get opposite sides on the horizontal Line situation, instead of always the same side, so I don't think that can be it.

 

EDIT:  Another possibility occurs to me -- are you in plan view in the WCS?  UCS, World.  Then PLAN, either current or World option.

Kent Cooper, AIA
0 Likes
Message 24 of 41

Anonymous
Not applicable

Yes sir,  the insertion points in all examples are correct. the trouble lies with the rotation. ANGBASE and/or ANGDIR System Variables are set to 0.

0 Likes
Message 25 of 41

ВeekeeCZ
Consultant
Consultant

@Kent1Cooper wrote:

... 

 

(defun C:TEST (/ esel ent edata pt1 pt2 side rot)

  (setq

    esel (entsel "\nSelect Line at which to add Shear Posts: ")

    ent (car esel)

    edata (entget ent)

    pt1 (cdr (assoc 10 edata)); start

    pt2 (cdr (assoc 11 edata)); end

    side (getpoint pt1 "\nPick to side of Line on which to add shear posts: ")

    rot (* (/ (angle (vlax-curve-getClosestPointTo ent side T) side) pi) 180); ROTation angle for Insert commands

  ); setq

  (command

    "_.insert" "ShearPost"

      "_none" (polar pt1 (angle pt1 pt2) 1.75); half-post-width in from start

      "" "" rot

    "_.insert" ""

      "_none" (polar pt2 (angle pt2 pt1) 1.75); from other end

      "" "" rot

  ); command

); defun


Nice... no need for if vertical or horizontal... I see, very nice. Works fine to me. I have no clue why it should not (all I can imagine has already been suggested). Sorry that I can not contribute to a solution.

0 Likes
Message 26 of 41

Anonymous
Not applicable

I wish I knew what it is that is making it not work on my end. Thanks to you both for help.

0 Likes
Message 27 of 41

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Yes sir,  the insertion points in all examples are correct. the trouble lies with the rotation. ANGBASE and/or ANGDIR System Variables are set to 0.


Just a grab-at-straws possibility --  You may have sent in Post 24 [above] before I got in the EDIT at the end of Post 23.  If you didn't notice that Edit yet [it wouldn't have caused the system to mark the thread as having something you hadn't seen yet], check into its WCS Plan-view question.  I ask only as a last desperate attempt, since I haven't seen any explicit confirmation about that part yet, but maybe you're already there.

Kent Cooper, AIA
0 Likes
Message 28 of 41

ВeekeeCZ
Consultant
Consultant

@Anonymous

 

If I may... I little modified Kents routine for test purpose and for trace values of variables. Then I attached a test file. Load TestTest.lsp, run it on a test file... Select marked points (as close as you can to pt1.1, then with osnap 1.2, then as close as pt1.2 and so on. Then go to AutoCAD, press F2 and CopyPaste commandline since first point......... as I did. Post a result in dwg and commandline list.

No need to say that it works fine to me.

0 Likes
Message 29 of 41

Anonymous
Not applicable


 

Just a grab-at-straws possibility --  You may have sent in Post 24 [above] before I got in the EDIT at the end of Post 23.  If you didn't notice that Edit yet [it wouldn't have caused the system to mark the thread as having something you hadn't seen yet], check into its WCS Plan-view question.  I ask only as a last desperate attempt, since I haven't seen any explicit confirmation about that part yet, but maybe you're already there.


I looked into it and I am at UCS World; Plan, World. 

 

 

0 Likes
Message 30 of 41

Anonymous
Not applicable

I hope this is helps...

0 Likes
Message 31 of 41

ВeekeeCZ
Consultant
Consultant

Not really. I really did not expect an error. And since I don't have a clue why (does not occur to me), I can't help.

0 Likes
Message 32 of 41

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

I hope this is helps...


Whereas BeekeeCZ's Command:-line trace, with the reporting that they built into the code, reads things like:

Select Line at which to add Shear Posts: (0.0 20.0 0.0)(10.0 20.0 0.0)
Pick to side of Line on which to add shear posts: (2.0 15.0 0.0)(2.0 20.0 0.0)4.71239270.0_.insert ....

 

yours reads things like:

 

Select Line at which to add Shear Posts: (-8.10417e-010 -6.1302 0.0)nil
Pick to side of Line on which to add shear posts: (2.0 15.0 0.0); error: bad argument value: AcDbCurve 47

 

The former is reporting both ends of the selected Line [p1 and p2], but the latter only one and a nil.  Then the former reports the side-selection point, the closest point on the Line, and the angle between those [rot], in its original radians and then converted to degrees., whereas yours reports only the side-selection point before the error message.

 

That nil suggests to me that your "line" isn't a Line entity, but perhaps a Polyline which has no code-11 entry in its entity data [hence the nil return].  It could be made to allow a Polyline, but it should presumably be limited to allow only a one-straight-segment Polyline [that can be done], and it would need to find the other end in a different way.

 

But why it fails on finding the closest point is harder to imagine.  The error message suggests that the selected object is not among those classified as "curves" [even if they're not curved] for purposes of (vlax-curve-...) functions, though I don't know what the 47 means.  It should be able to work with a Polyline, even if the p2 variable is nil, in which case I would expect it to run into trouble with the Insertions later, but not this early in the process.

 

I guess the first question is, what is the actual entity type of the "line" you're selecting?  It appears to be something that's not a "curve"-class object, and with a code-10 entry in its entity data but not a code-11 entry [which rules out, for example, Text or an Mline that would be examples of non-"curve"-class objects -- they have both 10 & 11 entries].

Kent Cooper, AIA
0 Likes
Message 33 of 41

ВeekeeCZ
Consultant
Consultant

@Kent1Cooper wrote:

@Anonymous wrote:

I hope this is helps...


...

 

I guess the first question is, what is the actual entity type of the "line" you're selecting?  ...


 

Are you selecting a LEADER intead of a line? That gives me the same error! 

0 Likes
Message 34 of 41

Anonymous
Not applicable

Fresh start this morning is showing great progress.. 

 

You are correct when I was working it yesterday I had osnaps on and was hitting the leaders that were there..  that caused the error.  

 

I ran it again this morning on the test file "BeeKee" sent me and it is inserting posts up and down and side to side....great!! 

 

The problem was having ortho and osnaps on.  feeling foolish but that clearly affecting my selections.

 

Having them turned off while this lisp runs will be great.   I would like to have the columns to land on "s-plan-rcol" but the lisp doesnt need to create the layer as it will already be there.  My intent is once this is all working right to copy and edit the lisp to have it do the same thing for different levels of the structure, i.e. s-plan-3col, s-plan-2col, etc.

 

Lastly, the error undo portion of the lisp.

 

 

You both are Lisp magicians.....and this is great!!!! 

 

 

0 Likes
Message 35 of 41

Kent1Cooper
Consultant
Consultant

Here's a version with all that stuff included [there are various different ways to do much of it, but this is the approach I usually take]:

 

(vl-load-com); if needed

(defun C:TEST (/ *error* doc svnames svvals esel ent edata pt1 pt2 side rot)
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg))
    ); if
    (mapcar 'setvar svnames svvals); reset System Variables
    (vla-endundomark doc)
    (princ)
  ); defun - *error*
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-startundomark doc)
  (setq ; System Variable saving/resetting without separate variables for each:
    svnames '(osmode cmdecho orthomode blipmode clayer); list of System Variable Names
    svvals (mapcar 'getvar svnames); get initial Values
    esel (entsel "\nSelect Line at which to add Shear Posts: ")
    ent (car esel)
    edata (entget ent)
    pt1 (cdr (assoc 10 edata)); start
    pt2 (cdr (assoc 11 edata)); end
    side (getpoint pt1 "\nPick to side of Line on which to add shear posts: ")
    rot (* (/ (angle (vlax-curve-getClosestPointTo ent side T)) pi) 180); ROTation angle for Insert commands
  ); setq
  (mapcar 'setvar svnames '(0 0 0 0 "s-plan-rcol"))
    ; turn off osnap, command echoing, ortho, blips; set Layer
  (command
    "_.insert" "ShearPost"
      "_none" (polar pt1 (angle pt1 pt2) 1.75); half-post-width in from start
      "" "" rot
    "_.insert" ""
      "_none" (polar pt2 (angle pt2 pt1) 1.75); from other end
      "" "" rot
  ); command
  (mapcar 'setvar svnames svvals); reset System Variables
  (vla-endundomark doc)
  (princ)
); defun

 

If you'd like it to restrict selection to a Line entity, and/or verify that something was in fact selected, and/or even ask the User to try again if they miss or pick something inappropriate, those can also be done.

Kent Cooper, AIA
Message 36 of 41

Anonymous
Not applicable

Having that limitation would likely be helpful

0 Likes
Message 37 of 41

Anonymous
Not applicable

I received an error:

 

 

Command: test
Select Line at which to add Shear Posts:
Pick to side of Line on which to add shear posts:
Error: too few arguments
Command:

 

 

 

0 Likes
Message 38 of 41

ВeekeeCZ
Consultant
Consultant
Same help as before, post 17 🙂
0 Likes
Message 39 of 41

Anonymous
Not applicable

Your the Best Coop.. thanks for your help in this. 

 

 

0 Likes
Message 40 of 41

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:
Same help as before, post 17 🙂

[My apologies for blindly adding to the original routine without checking for any follow-up about it like that, and thanks, BeekeeCZ, for finding that before I got around to it.]

Kent Cooper, AIA
0 Likes