Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Automatically assign layer to dimension based on dimstyle

9 REPLIES 9
Reply
Message 1 of 10
pb01eah
910 Views, 9 Replies

Automatically assign layer to dimension based on dimstyle

Hello everyone,

 

I'm currently facing a challenge with AutoCAD (version 2024) that I hope to solve with your expertise. I work extensively with different dimension styles (Dimstyles) in my drawings, and each of these styles is supposed to be associated with a specific layer. I am an apprentice, and when I ask for help, my coworkers don't even know what AutoLISP is.

For instance, dimensions with the style "COTE5" should automatically be placed on the layer "COT_DET_5".

 

However, the process I'm using requires manual selection of both the dimension style and its corresponding layer, which is quite time-consuming and prone to errors. My goal is to automate this process: when I draw dimensions using a specific style, I want AutoCAD to automatically assign the corresponding layer to these dimensions.

 

I've attempted to create an AutoLISP with ChatGpt but is not very effective and my understanding is very basic.

 

Here's a summary of what I'm trying to achieve:
1. Monitor when a dimension is drawn.
2. Check the dimension style of the newly drawn dimension.
3. Change the layer of the dimension to the predefined layer associated with that style.

 

I'm aware that is probably something that's is already done but i could'nt find a proper clue.

 

Could anyone provide guidance on how to approach this problem, or share if you've tackled similar automation tasks in AutoCAD? Any sample scripts or pointers to relevant AutoLISP functions and methodologies would be greatly appreciated.

 

Thank you in advance for your help and suggestions!

David

 

Labels (2)
9 REPLIES 9
Message 2 of 10
pendean
in reply to: pb01eah

For your item #1 and #2, aren't you actively selecting a different dimension style before you use it? Otherwise how are you jumping between the different dimension styles?

Or are you just using DIMCONTINUE command which by the way can be set to default to duplicate the dimension you are continuing as you asked (look up DIMCONTINUEMODE System Variable).

if you answered yes, are you familiar with DIMLAYER and -DIMSTYLE commands? You set the first to your desired layer and the second to your desired dimstyle before you create a dimension. I suspect this is the workflow you meant to ask for.

Try it all out at the commandline by just typing out what you are asking for to see what I mean.
Message 3 of 10
pb01eah
in reply to: pendean

Thank you for your suggestions. I've tried using DIMLAYER, but this sets the layer for all dimensions globally, which isn't quite what I need. ( I've discovered something wich is great 😄 )

I manually change between different dimension styles before using them. These dimensions are utilized for various scales, with each scale having its own dimension style and corresponding layer.

For example:

For scale 1:1, I use Dimstyle: COTE1 and Layer: JX_Cotdet_1_1.
For scale 1:5, I use Dimstyle: COTE5 and Layer: JX_Cotdet_1_5.
And so on for other scales...

The challenge arises because I have to draw in different scales within the same plan, which necessitates the ability to freeze layers in the layout. My goal is to automate the process of assigning the correct layer based on the selected dimension style, especially since each style is tied to a specific scale.
Message 4 of 10
pendean
in reply to: pb01eah


@pb01eah wrote:
Thank you for your suggestions. I've tried using DIMLAYER, but this sets the layer for all dimensions globally...

Well, not quite in your case: since you jump around different dimstyles before creating dimensions, the process of selecting the dimstyle coincides with with the dimlayer change too. One follows the other every single time.

 

And if you have a different dimension styles for each plot scale, don't you also have a need to change text styles every time you change plotscales too? How are those handled, don't they also need to be on separate layers corresponding to plot scale?


May I ask a side question: since we all do what you do all the time but not exactly your workflow, is this system a companywide methodology, a client required methodology, or just somethin you fell into over the years?

Annotative text/dimensions/blocks (and the added benefit of using layouts and viewports) have literally done away with your workflow for many of us over the last 20-ish years now.

Message 5 of 10
Sea-Haven
in reply to: pb01eah

A quick lisp lesson, you can make little functions like set dimstyle. You can save these say in a lisp along with other helper functions, then use appload yourlisp and add to the start up suite then it gets loaded every time you start Cad.

 

 

(defun c:d5 ( ) (command "._-dimstyle" "_restore" "Dimstyle 5")(setvar 'clayer "DIM5"))
(defun c:d6 ( ) (command "._-dimstyle" "_restore" "Dimstyle 6")(setvar 'clayer "DIM6"))
(defun c:d7 ( ) (command "._-dimstyle" "_restore" "Dimstyle 7")(setvar 'clayer "DIM7"))

 

 So as above type D6 and correct dimstyle is set and layer. 

Hope it makes sense. Yes it is simple and has no error checking about stuff missing.

Message 6 of 10
pb01eah
in reply to: pb01eah

@pendean

Hello pendean,

To clarify, I'll describe "our" way of working, perhaps you can advise on the "right way to do things." I work in a mid-sized metal construction company; I was in the workshops and was offered to move to the technical offices. We mainly make doors, windows, barriers, frames, and other traditional metal jobs. It's been 6 months since I started drawing on AutoCad.

 

We all share the same layers, the same Dimstyles, so we can easily continue someone else's work, at least in theory. Some don't use layers at all and just change colors while keeping the same layer, for example. Ultimately, everyone does a bit as they please. There's no real methodology to follow, and I learn by experimenting. The only thing is not to use annotative because most colleagues don't understand how to use it.

 

Regarding my workflow:
When I want to put dimensions at scale, I select my Dimstyle, draw , assign it the desired scale layer, and make it current if I need to continue. The same for arrows. In general, I assign the layer scale to dimensions and arrows.
For text, I manually change the font size according to the scale.

 

Hoping this helps you see the situation, if you have any "veteran" advice, I gladly welcome it 😄

 

@Sea-Haven  Hello 😮 ,

 

I've tried your code, and it works perfectly, which also helped me understand AutoLISP a bit better. Unfortunately, it's not exactly what I was looking for. After some research, it seems that what I want might be a bit too complex for me at the moment. Ideally, I'd like AutoCAD to automatically recognize when I use a dimension, check the current dimstyle, set the layer of the new object automatically to one I've chosen, and then revert to my previous layer before I drew the dimension.

 

For now, I've found a simpler solution: setting up palettes and using them. There, I can set up both the dimstyle and the layer. It requires a few more clicks, but it's good enough for now.




Message 7 of 10
Sea-Haven
in reply to: pb01eah

Hot of the press like a week old. We can discuss how I did it.

 

SeaHaven_0-1708061814172.png

 

 

Message 8 of 10
komondormrex
in reply to: pb01eah

hey there,

check this one. fill in your style-layer associations starting line 13, save, load both and start command-adding dimensions. 

 

 

 

 

;****************************************************************************************************************

(if (/= 'vlr-command-reactor  (type command_ended_reactor))
	(setq command_ended_reactor (vlr-command-reactor nil '((:vlr-commandended . command_ended))))
)

;****************************************************************************************************************

(defun command_ended (reactor_object parameter_list / dim_style_layer_assoc_list is_dim dim_dxf style_layer_pair)
	(setq dim_style_layer_assoc_list '(
								  ;	   dim_style  corresponding_layer
										("COTE5" . "COT_DET_5")
										;	...
									  )
	)
    (if (and
			(setq is_dim (wcmatch (cdr (assoc 0 (setq dim_dxf (entget (entlast))))) "*DIMENSION*"))
			(setq style_layer_pair (assoc (cdr (assoc 3 dim_dxf)) dim_style_layer_assoc_list))
		)
		(entmod (subst (cons 8 (cdr style_layer_pair)) (assoc 8 dim_dxf) dim_dxf))
		(if is_dim (alert (strcat "Paired layer for \"" (cdr (assoc 3 dim_dxf)) "\"dimstyle not set")))
	)
)

;****************************************************************************************************************

 

updated

 

Message 9 of 10
ВeekeeCZ
in reply to: pb01eah

@komondormrex  Wow! Really?? I would not expect something so bad from you.

 

 

I can imagine that something easy like this could serve the purpose.

 

(defun c:Load_DimStyleReactor nil
  (if *Reactor-DimStyle* (vlr-remove *Reactor-DimStyle*))
  (setq *Reactor-DimStyle* (vlr-sysvar-reactor nil '((:vlr-sysVarChanged . DimStyle-CallBack)))))

(defun c:Unload_DimStyleReactor nil (if *Reactor-DimStyle* (vlr-remove *Reactor-DimStyle*)) (princ))

(defun DimStyle-CallBack (rea lst / new)
  (and (= (car lst) "DIMSTYLE")
       (setq new (cond ((cdr (assoc (getvar 'dimstyle) '(("COTE1" . "JX_Cotdet_1_1")
							 ("COTE5" . "JX_Cotdet_1_5")
							 ))))
		       ("JX_Cotdet"))) ; name if style not match
       (/= (getvar 'dimlayer) new)
       (setvar 'dimlayer new)))

(c:Load_DimStyleReactor)  ; AUTOLOAD

 

Message 10 of 10
komondormrex
in reply to: ВeekeeCZ


@ВeekeeCZ wrote:

@komondormrex  Wow! Really?? I would not expect something so bad from you.


what are you talking about?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report