Jump to content

Looking for a LSP to select a dimension in the drawing and change the color to red for any type of dimension, be it a rotated, aligned, or angular.


Recommended Posts

Posted

Looking for a LSP to select a dimension in the drawing and change the color to red for any type of dimension, be it a rotated, aligned, or angular. I should be able to select more than one dimension from the drawing. It should not change the color of other objects in the drawing if many objects are selected.

Posted
5 minutes ago, Ashishs said:

Looking for a LSP to select a dimension in the drawing and change the color to red for any type of dimension, be it a rotated, aligned, or angular. I should be able to select more than one dimension from the drawing. It should not change the color of other objects in the drawing if many objects are selected.

 

Forget to mention, it should also change the color of the leader or multileader lines and arrows and the text. Thanks

Posted (edited)

Try this short untested routine...

 

(defun c:dims-lead_2_redcol ( / ss i dim dimx )
  (prompt "\nSelect dimensions and leaders(multileaders) on unlocked layer(s) to change their color to red...")
  (if (setq ss (ssget "_:L" (list (cons 0 "*DIMENSION,*LEADER"))))
    (repeat (setq i (sslength ss))
      (setq dim (ssname ss (setq i (1- i))))
      (setq dimx (entget dim))
      (if (assoc 62 dimx)
        (entupd (cdr (assoc -1 (entmod (subst (cons 62 1) (assoc 62 dimx) dimx)))))
        (entupd (cdr (assoc -1 (entmod (append dimx (list (cons 62 1)))))))
      )
    )
  )
  (princ)
)

 

Edited by marko_ribar
Posted
14 minutes ago, marko_ribar said:

Try this short untested routine...

 

(defun c:dims-lead_2_redcol ( / ss i dim dimx )
  (prompt "\nSelect dimensions and leaders(multileaders) on unlocked layer(s) to change their color to red...")
  (if (setq ss (ssget "_:L" (list (cons 0 "DIMENSION,*LEADER"))))
    (repeat (setq i (sslength ss))
      (setq dim (ssname ss (setq i (1- i))))
      (setq dimx (entget dim))
      (if (assoc 62 dimx)
        (entupd (cdr (assoc -1 (entmod (subst (cons 62 1) (assoc 62 dimx) dimx)))))
        (entupd (cdr (assoc -1 (entmod (append dimx (list (cons 62 1)))))))
      )
    )
  )
  (princ)
)

 

Sorry, its only working on multileader, not on leader, aligned, rotated, angular, radial dimesions etc

Posted

@Ashishs

Give this version a try:

;; Written by PJK 11/26/2024
;; Topic: https://www.cadtutor.net/forum/topic/94150-looking-for-a-lsp-to-select-a-dimension-in-the-drawing-and-change-the-color-to-red-for-any-type-of-dimension-be-it-a-rotated-aligned-or-angular/
(defun c:dimred (/ e e2 i l l2 ss)
   (if (setq ss (ssget '((0 . "DIMENSION,*LEADER"))))
      (repeat (setq i (sslength ss))
         (setq e (ssname ss (setq i (1- i)))
               l (entget e)
         )
         (if (wcmatch (cdr (assoc 0 l)) "DIMENSION,LEADER,QLEADER")
            (progn
               (command "._dimoverride" "_dimclre" 1 "_dimclrd" 1 "_dimclrt" 1 "" e "")
               (if (assoc 340 l)
                  (progn
                     (setq l2 (entget (setq e2 (cdr (assoc 340 l)))))
                     (if (assoc 62 l2)
                        (progn (entmod (subst (cons 62 1) (assoc 62 l2) l2))(entupd e2))
                        (progn (entmod (append l2 (list (cons 62 1))))(entupd e2))
                     )
                  )
               )
            )
            (if (assoc 62 l)
               (progn (entmod (subst (cons 62 1) (assoc 62 l) l))(entupd e))
               (progn (entmod (append l (list (cons 62 1))))(entupd e))
            )
         )
      )
   )
   (princ)
)

 

  • Like 2
Posted

I've changed filter...

Changed this : (list (cons 0 "DIMENSION,*LEADER"))

To this : (list (cons 0 "*DIMENSION,*LEADER"))

 

Added * in front of "DIMENSION"... Still untested, but should work and for dimensions...

Posted (edited)

Works very well on all dimension types. Thanks a ton.

Edited by Ashishs
  • Like 1
Posted
56 minutes ago, marko_ribar said:

I've changed filter...

Changed this : (list (cons 0 "DIMENSION,*LEADER"))

To this : (list (cons 0 "*DIMENSION,*LEADER"))

 

Added * in front of "DIMENSION"... Still untested, but should work and for dimensions...

Sorry, can you paste the corrected lisp here, please?

Posted
1 minute ago, Ashishs said:

Works very well on all dimension types. Thanks a ton.

If your are referring to my solution. Please give my post a "like" or "Thanks" so I know that it was mine 🙂

Posted
1 hour ago, pkenewell said:

@Ashishs

Give this version a try:

;; Written by PJK 11/26/2024
;; Topic: https://www.cadtutor.net/forum/topic/94150-looking-for-a-lsp-to-select-a-dimension-in-the-drawing-and-change-the-color-to-red-for-any-type-of-dimension-be-it-a-rotated-aligned-or-angular/
(defun c:dimred (/ e e2 i l l2 ss)
   (if (setq ss (ssget '((0 . "DIMENSION,*LEADER"))))
      (repeat (setq i (sslength ss))
         (setq e (ssname ss (setq i (1- i)))
               l (entget e)
         )
         (if (wcmatch (cdr (assoc 0 l)) "DIMENSION,LEADER,QLEADER")
            (progn
               (command "._dimoverride" "_dimclre" 1 "_dimclrd" 1 "_dimclrt" 1 "" e "")
               (if (assoc 340 l)
                  (progn
                     (setq l2 (entget (setq e2 (cdr (assoc 340 l)))))
                     (if (assoc 62 l2)
                        (progn (entmod (subst (cons 62 1) (assoc 62 l2) l2))(entupd e2))
                        (progn (entmod (append l2 (list (cons 62 1))))(entupd e2))
                     )
                  )
               )
            )
            (if (assoc 62 l)
               (progn (entmod (subst (cons 62 1) (assoc 62 l) l))(entupd e))
               (progn (entmod (append l (list (cons 62 1))))(entupd e))
            )
         )
      )
   )
   (princ)
)

 

Sorry, this is not working on the attached Mulileader dimension type. Please check the attacehd drawing.

Multileader.dwg

Posted
4 hours ago, Ashishs said:

Sorry, this is not working on the attached Mulileader dimension type. Please check the attacehd drawing.

@Ashishs

OK. Multileaders are a bit harder. With the code below. I can set the leader line color different than the Mleaderstyle, but I don't know how to change the implicit color of the text (also set in the Mleaderstyle), other than to put a color formatting code into the text content. If someone else knows how to change the Text color outside of the Mleaderstyle, then I welcome their input. Try this new version:

(defun c:dimred (/ co e e2 i l l2 o ss)
   (if (setq ss (ssget '((0 . "DIMENSION,*LEADER"))))
      (repeat (setq i (sslength ss))
         (setq e (ssname ss (setq i (1- i)))
               l (entget e)
         )
         (cond
            ((wcmatch (cdr (assoc 0 l)) "DIMENSION,LEADER,QLEADER")
               (command "._dimoverride" "_dimclre" 1 "_dimclrd" 1 "_dimclrt" 1 "" e "")
               (if (assoc 340 l)
                  (progn
                     (setq l2 (entget (setq e2 (cdr (assoc 340 l)))))
                     (if (assoc 62 l2)
                        (progn (entmod (subst (cons 62 1) (assoc 62 l2) l2))(entupd e2))
                        (progn (entmod (append l2 (list (cons 62 1))))(entupd e2))
                     )
                  )
               )
            )
            ((= (cdr (assoc 0 l)) "MULTILEADER")
               (if (assoc 62 l)
                  (progn (entmod (subst (cons 62 1) (assoc 62 l) l))(entupd e))
                  (progn (entmod (append l (list (cons 62 1))))(entupd e))
               )
               (setq o (vlax-ename->vla-object e)
                     co (vla-get-leaderlinecolor o)
               )
               (vla-put-colorindex co 1)
               (vla-put-leaderlinecolor o co)
               (vla-put-textstring o
                   (strcat "\\C1;"
                      (vla-get-textstring o)
                   )
               )
               
            )
         )
      )
   )
   (princ)
)

 

FWIW - with Mleaders, you could create a style that is all RED, then easily use properties to change the Mleader style.

 

  • Like 1
Posted
10 hours ago, pkenewell said:

@Ashishs

OK. Multileaders are a bit harder. With the code below. I can set the leader line color different than the Mleaderstyle, but I don't know how to change the implicit color of the text (also set in the Mleaderstyle), other than to put a color formatting code into the text content. If someone else knows how to change the Text color outside of the Mleaderstyle, then I welcome their input. Try this new version:

(defun c:dimred (/ co e e2 i l l2 o ss)
   (if (setq ss (ssget '((0 . "DIMENSION,*LEADER"))))
      (repeat (setq i (sslength ss))
         (setq e (ssname ss (setq i (1- i)))
               l (entget e)
         )
         (cond
            ((wcmatch (cdr (assoc 0 l)) "DIMENSION,LEADER,QLEADER")
               (command "._dimoverride" "_dimclre" 1 "_dimclrd" 1 "_dimclrt" 1 "" e "")
               (if (assoc 340 l)
                  (progn
                     (setq l2 (entget (setq e2 (cdr (assoc 340 l)))))
                     (if (assoc 62 l2)
                        (progn (entmod (subst (cons 62 1) (assoc 62 l2) l2))(entupd e2))
                        (progn (entmod (append l2 (list (cons 62 1))))(entupd e2))
                     )
                  )
               )
            )
            ((= (cdr (assoc 0 l)) "MULTILEADER")
               (if (assoc 62 l)
                  (progn (entmod (subst (cons 62 1) (assoc 62 l) l))(entupd e))
                  (progn (entmod (append l (list (cons 62 1))))(entupd e))
               )
               (setq o (vlax-ename->vla-object e)
                     co (vla-get-leaderlinecolor o)
               )
               (vla-put-colorindex co 1)
               (vla-put-leaderlinecolor o co)
               (vla-put-textstring o
                   (strcat "\\C1;"
                      (vla-get-textstring o)
                   )
               )
               
            )
         )
      )
   )
   (princ)
)

 

FWIW - with Mleaders, you could create a style that is all RED, then easily use properties to change the Mleader style.

 

This solutions is working for me so far, even on the mulileaders. Thanks a lot.

  • Like 1
Posted

If I have to change color to blue instead of red, which are the parameters I need to change in the above routine, please?

Posted (edited)

Colours are set as numbers, 1 for red, 2 for blue and so on up to 256 for the index colour... so change colour '1' to whatever colour you want in the above.

 

In pkenewells code you might be looking for lines such as:

(vla-put-colorindex co 1)

(progn (entmod (subst (cons 62 1) (assoc 62 l) l))(entupd e))

(progn (entmod (subst (cons 62 1) (assoc 62 l2) l2))(entupd e2))

(command "._dimoverride" "_dimclre" 1 "_dimclrd" 1 "_dimclrt" 1 "" e "")

 

c:dimred to c:dimblue of course

 

changing 1 to what you want it to be. If it was me for future versatility I might set the value, 1, above as a variable and set that variable at the start of the routine - that way you only need to make one change to change them all. Then you can just copy, paste, adjust to any colour you like.

 

 

 

pkenewell... I might have something that changes leader text colour will have a think this afternoon

 

Quick nentsel on a leader:

(304 . "{\\C6;Here is a leader}")

Colour code might be in the text

Edited by Steven P
  • Like 1
Posted (edited)
1 hour ago, Ashishs said:

If I have to change color to blue instead of red, which are the parameters I need to change in the above routine, please?

@Ashishs Try the updated code below. You can set the variable at the top (see comments in code) to the ACI color number (5 is BLUE) to what you want.

(setq cl 5); 1=RED, 2=YELLOW, 3=GREEN, 4=CYAN, 5=BLUE, 6=MAGENTA etc...

Optionally, you can uncomment the line below it (remove the ";" in front) to interactively select the color you want at the start. NOTE - this will not use RGB True colors or Colorbook colors, only ACI colors.

;|==============================================================
dimcol.lsp by Phil Kenewell - 11/25/2024

Description:
   By request of a user on CADTutor.com
   https://www.cadtutor.net/forum/topic/94150-looking-for-a-lsp-to-select-a-dimension-in-the-drawing-and-change-the-color-to-red-for-any-type-of-dimension-be-it-a-rotated-aligned-or-angular/

   This program allows you to change the color of all selected Dimensions
   and Leaders, QLeader, Mleaders.
   
Last Update:
   11/27/2024 = Added variable cl and (acad_colordlg) to select the
                color at start instead of hardcoding.
===============================================================|;
(defun c:dimcol (/ cl co e e2 i l l2 o ss)
   
   ;-----------------------------
   ;; Change this variable to Change the color. ACI color number integer:
   (setq cl 5); 1=RED, 2=YELLOW, 3=GREEN, 4=CYAN, 5=BLUE, 6=MAGENTA etc...
   ;(setq cl (acad_colordlg cl)); Alternately select color at beginning.
   ;---------------------------------------------------------------------
   (command "._undo" "_Begin")
   (if (setq ss (ssget '((0 . "DIMENSION,*LEADER"))))
      (repeat (setq i (sslength ss))
         (setq e (ssname ss (setq i (1- i)))
               l (entget e)
         )
         (cond
            ((wcmatch (cdr (assoc 0 l)) "DIMENSION,LEADER,QLEADER")
               (command "._dimoverride" "_dimclre" cl "_dimclrd" cl "_dimclrt" cl "" e "")
               (if (assoc 340 l)
                  (progn
                     (setq l2 (entget (setq e2 (cdr (assoc 340 l)))))
                     (if (assoc 62 l2)
                        (progn (entmod (subst (cons 62 cl) (assoc 62 l2) l2))(entupd e2))
                        (progn (entmod (append l2 (list (cons 62 cl))))(entupd e2))
                     )
                  )
               )
            )
            ((= (cdr (assoc 0 l)) "MULTILEADER")
               (if (assoc 62 l)
                  (progn (entmod (subst (cons 62 cl) (assoc 62 l) l))(entupd e))
                  (progn (entmod (append l (list (cons 62 cl))))(entupd e))
               )
               (setq o  (vlax-ename->vla-object e)
                     co (vla-get-leaderlinecolor o)
               )
               (vla-put-colorindex co cl)
               (vla-put-leaderlinecolor o co)
               (vla-put-textstring o
                   (strcat "\\C" (itoa cl) ";"
                      (vla-get-textstring o)
                   )
               )
               
            )
         )
      )
   )
   (command "._Undo" "_End")
   (princ)
)

 

Edited by pkenewell
Added a Header to the routine.
  • Like 1
Posted (edited)
1 hour ago, Steven P said:

pkenewell... I might have something that changes leader text colour will have a think this afternoon

 

Quick nentsel on a leader:

(304 . "{\\C6;Here is a leader}")

Colour code might be in the text

@Steven P

Yeah, right now I am adding the formatting code in the text, but its a problem If you use it multiple times. It will currently just stack color codes in. I could search for them and remove old color codes, but the code could get a bit lengthy for this; it depends on the OP's intent. Another way would be to copy the style of the Mleader, then make a new style and set all the colors, then change the Mleader to the new style.

There is probably some way to dig through the DXF codes to bypass the Mleader Style Text color, but I haven't had time yet to dig. 🙂

 

EDIT - I think I found it - DXF code 90 in the context data. I'll have to try it and get back. I will still have to strip formatting color codes out of the text. This code keeps getting bigger LOL.

Edited by pkenewell
  • Like 2
Posted (edited)
35 minutes ago, pkenewell said:

@Ashishs Try the updated code below. You can set the variable at the top (see comments in code) to the ACI color number (5 is BLUE) to what you want.

(setq cl 5); 1=RED, 2=YELLOW, 3=GREEN, 4=CYAN, 5=BLUE, 6=MAGENTA etc...

Optionally, you can uncomment the line below it (remove the ";" in front) to interactively select the color you want at the start. NOTE - this will not use RGB True colors or Colorbook colors, only ACI colors.

(defun c:dimcol (/ cl co e e2 i l l2 o ss)
   
   ;-----------------------------
   ;; Change this variable to Change the color. ACI color number integer:
   (setq cl 5); 1=RED, 2=YELLOW, 3=GREEN, 4=CYAN, 5=BLUE, 6=MAGENTA etc...
   ;(setq cl (acad_colordlg cl)); Alternately select color at beginning.
   ;---------------------------------------------------------------------
   (command "._undo" "_Begin")
   (if (setq ss (ssget '((0 . "DIMENSION,*LEADER"))))
      (repeat (setq i (sslength ss))
         (setq e (ssname ss (setq i (1- i)))
               l (entget e)
         )
         (cond
            ((wcmatch (cdr (assoc 0 l)) "DIMENSION,LEADER,QLEADER")
               (command "._dimoverride" "_dimclre" cl "_dimclrd" cl "_dimclrt" cl "" e "")
               (if (assoc 340 l)
                  (progn
                     (setq l2 (entget (setq e2 (cdr (assoc 340 l)))))
                     (if (assoc 62 l2)
                        (progn (entmod (subst (cons 62 cl) (assoc 62 l2) l2))(entupd e2))
                        (progn (entmod (append l2 (list (cons 62 cl))))(entupd e2))
                     )
                  )
               )
            )
            ((= (cdr (assoc 0 l)) "MULTILEADER")
               (if (assoc 62 l)
                  (progn (entmod (subst (cons 62 cl) (assoc 62 l) l))(entupd e))
                  (progn (entmod (append l (list (cons 62 cl))))(entupd e))
               )
               (setq o  (vlax-ename->vla-object e)
                     co (vla-get-leaderlinecolor o)
               )
               (vla-put-colorindex co cl)
               (vla-put-leaderlinecolor o co)
               (vla-put-textstring o
                   (strcat "\\C" (itoa cl) ";"
                      (vla-get-textstring o)
                   )
               )
               
            )
         )
      )
   )
   (command "._Undo" "_End")
   (princ)
)

 

Working perfectly except for multileader dimensions, which I can manage manually also. Thanks a lot. Attached is a drawing showing multileader tex no changed color for your reference.

Multileader.dwg

Edited by Ashishs
Posted
40 minutes ago, Ashishs said:

Working perfectly except for multileader dimensions, which I can manage manually also. Thanks a lot.

The problem is the internal formatting codes in the MLeader Text. See the properties panel in the Screenshot. See all the "\Cx;" codes? They have to be stripped out before using the DIMCOL command again:

image.png.39174a43921a02a340297da111c2844f.png

 

I'm working on a way to find these codes and strip them out before applying the color to the text again.

Posted (edited)

@Ashishs This is not perfect, but it attempts to strip out any color codes from the Text in the Mleader before replacing with the new color code. Try the updated code.

;|==============================================================
dimcol.lsp by Phil Kenewell - 11/25/2024

Description:
   By request of a user on CADTutor.com
   https://www.cadtutor.net/forum/topic/94150-looking-for-a-lsp-to-select-a-dimension-in-the-drawing-and-change-the-color-to-red-for-any-type-of-dimension-be-it-a-rotated-aligned-or-angular/

   This program allows you to change the color of all selected Dimensions
   and Leaders, QLeader, Mleaders.
   
Last Update:
   11/27/2024 = Added variable cl and (acad_colordlg) to select the
                color at start instead of hardcoding.
===============================================================|;

(defun c:dimcol (/ cl co e e2 i l l2 n o ss tx)
   (vl-load-com)
   ;-----------------------------
   ;; Change this variable to Change the color. ACI color number integer:
   (setq cl 5); 1=RED, 2=YELLOW, 3=GREEN, 4=CYAN, 5=BLUE, 6=MAGENTA etc...
   ;(setq cl (acad_colordlg cl)); Alternately select color at beginning.
   ;---------------------------------------------------------------------
   (command "._undo" "_Begin")
   (if (setq ss (ssget '((0 . "DIMENSION,*LEADER"))))
      (repeat (setq i (sslength ss))
         (setq e (ssname ss (setq i (1- i)))
               l (entget e)
         )
         (cond
            ((wcmatch (cdr (assoc 0 l)) "DIMENSION,LEADER,QLEADER")
               (command "._dimoverride" "_dimclre" cl "_dimclrd" cl "_dimclrt" cl "" e "")
               (if (assoc 340 l)
                  (progn
                     (setq l2 (entget (setq e2 (cdr (assoc 340 l)))))
                     (if (assoc 62 l2)
                        (progn (entmod (subst (cons 62 cl) (assoc 62 l2) l2))(entupd e2))
                        (progn (entmod (append l2 (list (cons 62 cl))))(entupd e2))
                     )
                  )
               )
            )
            ((= (cdr (assoc 0 l)) "MULTILEADER")
               (if (assoc 62 l)
                  (progn (entmod (subst (cons 62 cl) (assoc 62 l) l))(entupd e))
                  (progn (entmod (append l (list (cons 62 cl))))(entupd e))
               )
               (setq o  (vlax-ename->vla-object e)
                     co (vla-get-leaderlinecolor o)
                     tx (vla-get-textstring o)
               )
               (vla-put-colorindex co cl)
               (vla-put-leaderlinecolor o co)
               (while (wcmatch tx "*\\C*;*")
                  (setq n 0)
                  (repeat 257
                     (setq tx (vl-string-subst "" (strcat "\\C" (itoa n) ";") tx)
                            n (1+ n)
                     )
                  )
               )
               (vla-put-textstring o
                   (strcat "\\C" (itoa cl) ";" tx)
               )
               
            )
         )
      )
   )
   (command "._Undo" "_End")
   (princ)
)

 

Edited by pkenewell
Added (vl-load-com) just to be safe.
  • Like 2
Posted (edited)

Edit: Wasn't working....

 

See what you are doing pkenewell, but the colour codes might need to remove the paired { } - could be a situation where formats overlap (colour, underline, italic.... ) which for a dimension, leader etc is a slim chance from a draughtsman who maybe should be shot, but still a slim chance. The wrong closing } will close the wrong format.

 

 

'Wasn't working' was a modified Lee Mac Un-format for just the colour codes but the paired { } wasn't quite working right, Could do a modified StripMText to take out the interface and just colours.. but a lot more code in there

 

 

 

 

Edit again... for reference.... might just work... and the irony of reference - I have lost the reference where I got this from.

 

(defun c:NoColMT ( / ) ; No Colour MText
  (NoForMT (strcase (getstring "\nEnter Code (C, T, H, W, Q)")))
  (princ)
)
(defun NoForMt ( MyCode / MyEnt ed MyString SwapSlashes StrPos ColonPos ColourCode) ; C, T, H, W, Q
;;Slight discrepancy, opening '{' is retained at first formatting point
  (setq MyEnt (car (entsel)))        ;;Select single text entity
  (setq ed (entget MyEnt))           ;;Entity Definition
  (setq MyString (cdr (assoc 1 ed))) ;;Extract Text
  (setq SwapSlashes "ADDINGINBLACKSLASH")

  (while (wcmatch MyString "*\\\\*" )(setq MyString (vl-string-subst SwapSlashes "\\\\" MyString) )) ;;hide \\\\

  (if (and (wcmatch MyString (strcat "*{*`\\" MyCode "*}*")) (= (cdr (assoc 0 ed)) "MTEXT") )
    (progn
      (while (wcmatch MyString (strcat "*{*`\\" MyCode "*}*"))
        (setq StrPos (vl-string-search (strcat "\\" MyCode) MyString ) )
        (setq ColonPos (+ (vl-string-search ";" MyString (+ StrPos 3)) 2))
        (setq ColourCode (substr MyString (+ StrPos 1) (- ColonPos (+ StrPos 1)) ) )
        (setq MyString (vl-string-subst "" ColourCode MyString) )
      ) ; end while
      (setq ed (subst (cons 1 MyString) (assoc 1 ed) ed ))     ;; text String <~250 characters
      (entmod ed)                           ;; Modify & update entity
    ) ; end progn
  ) ; end if
  (while (wcmatch MyString (strcat "*" SwapSlashes "*") )(setq MyString (vl-string-subst "\\\\" SwapSlashes MyString) ))
)

 

Edited by Steven P
  • Like 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...