Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Put inserted block on same layer as selected object

VoDich2014
Explorer

Put inserted block on same layer as selected object

VoDich2014
Explorer
Explorer

Need help with routine to insert a block on a CAD object and have its layer, linetype, and color be the same as what ever the selected CAD object is.

 

My current process involves the 2 steps below:

1) Call the routine to insert a block on the current layer:

 

(defun c:V_flowarrow ()
(setq mosmode (getvar "osmode"))
(setq mdimscale (getvar "dimscale"))
(command "-insert" "V_flowarrow.dwg" pause mdimscale "" pause)
(setvar "osmode" mosmode)
(princ)
)

 

The block used in this example is V_flowarrow.dwg that is drawn using only layer 0. This is just one of the many blocks I normally use; the routine is pretty much the same. The user picks an insertion point (this point is usually on a line or pline, but could possibly be any CAD objects, and the osnap setting can be endpoint, midpoint, or nearest). The user picks another point, usually another point on the line, pline, or could possibly be any CAD objects on a drawing to set the rotation angle of the block.

 

2) Match the properties of the line, pline, or CAD object to the block so that the object and the inserted block are on the same layer.

 

Could someone help modifying the above routine to simplify this process so that the inserted block will inherit properties of the selected CAD object, including the angle if possible, so that the user would just have to call the routine and pick an insertion point?

 

Any help is greatly appreciated.  Thank you in advance!!

0 Likes
Reply
Accepted solutions (2)
701 Views
14 Replies
Replies (14)

CADaSchtroumpf
Advisor
Advisor

A more generic procedure if that can inspire you...
This launches the command concerning an already drawn object by setting its properties as the object under the cursor (see the info appearing in the info bar instead of the coordinates)

0 Likes

whitney_jeff
Collaborator
Collaborator

As far as the angle, that will also depend on which direction the line or pline was drawn in originally.  It might be best to allow the user to select the angle

0 Likes

Kent1Cooper
Consultant
Consultant

It doesn't change the OSMODE setting, so what is the purpose of saving that first and resetting it later?  Should it be changing it before the Insert?

 

I think, since Osnapping to some of your Osnap choices could be at a location with more than one object, that the solution would probably involve selecting the object on which to place it, even though the Inserting would actually land at the Osnap location.  That way, a simple MATCHPROP after Inserting can take care of the Layer, etc.

Kent Cooper, AIA
0 Likes

ronjonp
Advisor
Advisor

@VoDich2014 

For the rotation have you thought about adding an alignment parameter?

0 Likes

VoDich2014
Explorer
Explorer

CADaSchtroumpf: I'm not sure what to do with that routine. I know how to load it, but that is as far as I can take it....programming is way too advanced for me to figure out what to do.

 

Jeff: A valid point.

 

Kent: Some of my other routines force the user to insert the block at either a midpoint, endpoint, or intersection, so I set the osmode setting before the insert command. But you are right that it's not needed for this example. Now you got me wondering if I even need the osmode setting, even if the routine forces the user to pick just a midpoint or endpoint. Thanks for pointing that out... I'm still a newbie with this.

 

I'm not too worry about having more than one object. So MATCHPROP after the block insert? I'm trying to minimize the number of mouse clicks if possible. MATCHPROP looks for "source" and "destination" from the user. When the user selects an insertion point for the block on a CAD object (for simplicity, there's only 1 CAD object), is it possible to set that CAD object as a "source" for MATCHPROP?

 

Ron: I have not....I'm still too new with LISP.

 

Thank you all for your input.

0 Likes

Sea-Haven
Mentor
Mentor

For me pick object to match 1st set layer, then insert block, if objects inside block are on layer 0 then properties will match.

 

Need a what is 1st object then can do the block rotation and position to suit. 

 

May be a dwg would help.

 

0 Likes

ВeekeeCZ
Consultant
Consultant

Possibly like this. 

About rotation. As it's written, it uses the last rotation automatically (0 if the first time). You can still type R to change it.

 

(defun c:V_flowarrow ( / e el o)
  
  (if (setq e (car (entsel "\nSelect property source object: ")))
    (progn
      (setq o (mapcar 'getvar '("clayer" "cecolor" "celtype"))) 	; save old sysvar values

      (setvar 'clayer (cdr (assoc 8 (entget e))))		
      (setvar 'cecolor (itoa (cdr (assoc 62 (entget e)))))
      (setvar 'celtype (cdr (assoc 6 (entget e))))
      (or *ib-r* (setq *ib-r* 0.))

      (setq el (entlast))
      (command-s "_.-insert" "V_flowarrow" "_s" (getvar 'dimscale) "_r" (angtos 0. (getvar 'aunits) 9))
      (if (not (equal el (entlast))) (setq *ib-r* (cdr (assoc 50 (entget (entlast)))))) 		; save ang of block just inserted
				   
      (mapcar 'setvar '(clayer cecolor celtype) o)
      ))
(princ)
)

 

0 Likes

Kent1Cooper
Consultant
Consultant
Accepted solution

@VoDich2014 wrote:

....

I'm not too worry about having more than one object. So MATCHPROP after the block insert? I'm trying to minimize the number of mouse clicks if possible. MATCHPROP looks for "source" and "destination" from the user. When the user selects an insertion point for the block on a CAD object (for simplicity, there's only 1 CAD object), is it possible to set that CAD object as a "source" for MATCHPROP?

....


EDIT:  If there won't be anything else, or if it might be at an Endpoint that meets another object on the same Layer [e.g. a corner between two Lines], then a crossing-window selection using the point just used for the insertion would find the new Block IF it has a drawn element through its insertion point, and also the object(s) Osnapped to.  The new Block can be removed from the selection set if it's there [it won't matter if it isn't], and the [an?] object Osnapped to would then be the first item in it.  This should work [untested]:

 

(defun c:V_flowarrow (/ ss)
  (command "-insert" "V_flowarrow.dwg" "_scale" (getvar "dimscale") pause pause)
  (setq ss (ssget "_C" (getvar 'lastpoint) (getvar 'lastpoint)))
  (ssdel (entlast) ss); if Block has drawn element at insertion point and is in selection
  (command "_.matchprop" (ssname ss 0) (entlast) "")
  (princ)
)

 

Further EDIT:  This assumes that the Osnapped location is one that is actually on the drawn part of the object [END, MID, INT, etc.], not one that is elsewhere [CEN, GCEN, INS or PER in some cases, etc.].

Kent Cooper, AIA
0 Likes

VoDich2014
Explorer
Explorer

When I run the routine, it prompts "Select property source object: ".  But when I select an object, it says "Select property source object: bad argument type: fixnump: nil"

0 Likes

VoDich2014
Explorer
Explorer

Thank you Kent !!  The routine works great.

 

Edit:  After the routine, the command window shows:

Command: V_FLOWARROW
Current active settings: Color Layer Ltype Ltscale Lineweight Transparency Thickness PlotStyle Dim Text Hatch Polyline Viewport Table Material Multileader Center object

 

Anyway to not display current active settings?

 

Could you please clarify your first edit?


@Kent1Cooper wrote:

EDIT:  ... then a crossing-window selection using the point just used for the insertion would find the new Block IF it has a drawn element through its insertion point, and also the object(s) Osnapped to.  The new Block can be removed from the selection set if it's there [it won't matter if it isn't], and the [an?] object Osnapped to would then be the first item in it.


For instances where I have multiple objects at the insertion point like the image below, the user can just manually match properties.

VoDich2014_0-1663882517356.png

 

 

0 Likes

Kent1Cooper
Consultant
Consultant
Accepted solution

@VoDich2014 wrote:

....  After the routine, the command window shows:

... Current active settings: Color Layer ....

Anyway to not display current active settings?

 

Could you please clarify your first edit?

....

For instances where I have multiple objects at the insertion point like the image below, the user can just manually match properties.


That message is a part of the MATCHPROP prompts, and apparently cannot be suppressed [I tried both CMDECHO and NOMUTT settings].  If you want some limited set of properties matched to the Osnapped-to object [item 2) in Message 1 suggests only the Layer, but maybe you would want Layer and color, for instance], that could be done without the use of a MATCHPROP command, eliminating that message.

 

The situation in your image is what this from Message 6:

  I'm not too worry about having more than one object.

suggested is not likely.  If it's more likely than you thought, the routine could be made to check how many objects remain in the selection set after the Block has been removed, and if more than one, ask the User to pick which one they want the Block to be matched to.

 

EDIT:  For Layer-only match, incorporating both thoughts:

(defun c:V_flowarrow (/ ss source)
  (command "_.insert" "V_flowarrow" "_scale" (getvar "dimscale") pause pause)
  (setq ss (ssget "_C" (getvar 'lastpoint) (getvar 'lastpoint))); select everything there
  (ssdel (entlast) ss); if Block has drawn element at insertion point and is selected
  (setq source (if (= (sslength ss) 1) (ssname ss 0) (car (entsel "\nSelect object to match Layer of: "))))
  (setpropertyvalue (entlast) "LayerID" (getpropertyvalue source "LayerID"))
  (princ)
)

[I also eliminated the unnecessary ".dwg" filetype ending on the Block name.  If the Block name is already in the drawing, that is ignored -- it doesn't go looking for an external drawing file -- and if not, it doesn't need it -- it won't look for any other file type.]

Kent Cooper, AIA

VoDich2014
Explorer
Explorer

Amazing....I like it!! Thanks for the clarification and the ".dwg" tip. That makes sense now. I like this routine with the option to pick which object to match to better.

 

Thank you master Kent!

0 Likes

Kent1Cooper
Consultant
Consultant

@VoDich2014 wrote:

Amazing....I like it!! Thanks for the clarification and the ".dwg" tip. That makes sense now. I like this routine with the option to pick which object to match to better.

 

Thank you master Kent!


You're welcome [Accepted Solution, perhaps?].  A further possible enhancement that I could steal from another routine I have, if you like:  check whether there is either a Block with that name already in the drawing, or a drawing with that name in some location where AutoCAD knows to look for it, and if neither, alert the User and quit the routine.  And one can often think of other possible refinements, such as: to set a specific set of Osnap modes for you, and restore the previous setting afterwards; and if it does something like that, *error* handling to ensure that gets set back if something goes wrong;  and to ensure the current Layer is not locked [which would prevent the Layer change of the Block];  etc.

Kent Cooper, AIA
0 Likes

spambox2018
Explorer
Explorer

Thank you so much for this routine.  It has saved me so much time.  A request if I may, instead of inserting a block, can I just start drawing lines or plines and have the drawn line/plines from that one line/pline command be on the same layer as the already drawn line on the drawing?

 

spambox2018_0-1666133719982.png

 

Please see the sample picture above and the 3 different scenarios below.

The green and cyan lines are already on the drawing.  Current layer could be anything.

Scenario 1:  Line or pline command started.  Starting at point 1 and ending with point 4.  Once line or pline command is completed, the 3 lines drawn will match properties of the green line.

 

Scenario 2:  Similar to scenario 1, except that the last point is on the green line.  I can see that this scenario might be a bit complicated or messy.

 

Scenario 3:  Similar to scenario 1, except that point 1 is at an intersection where there are 2 or more objects, so the user will have to choose which object to match to, similar to Kent's routine with the inserted block.

 

I really like what Kent's routine is doing to the inserted block.  I'm not sure how or what to modify to make it work on the lines drawn.  I'm sorry if this is not the correct place to make this request.

0 Likes

Type a product name