Unassign Material from Active Part and Change DimStyle Using AutoLISP or VBA

Unassign Material from Active Part and Change DimStyle Using AutoLISP or VBA

Anonymous
Not applicable
1,066 Views
14 Replies
Message 1 of 15

Unassign Material from Active Part and Change DimStyle Using AutoLISP or VBA

Anonymous
Not applicable

Hello,

 

Product:  AutoCAD, AutoCAD Mechanical 2009 or Mechanical Desktop 2009 (soln for either would be great)

 

1) I have parts that have a material assigned to them through the 'MASSPROPS' dialog box.  When assigning the material to a part, material properties associated with that part are assigned as well.  Is there a way to either remove the material assignment or create a "dummy" material with material property values set to 0 and assign this new material to the part using AutoLISP?  If not AutoLISP, is it possible using any other code type?

 

2) Is there a good example out there somewhere demonstrating how to access and change drawing dimension styles using AutoLISP?  Right now I have dimensions with many different styles. 

 

My goal:  

- Create 2 dimension styles if they do not already exist:  CustFract and CustDec

- All dimensions that have a primary unit style of "Fraction" would be set to dimension style "CustFract".  Any other dimensions would be set to "CustDec".

 

 

Thanks,

Candace 

0 Likes
Accepted solutions (1)
1,067 Views
14 Replies
Replies (14)
Message 2 of 15

elshawadfy
Collaborator
Collaborator

1- I havent worked with Autocad Elec, so ican't realy say any thing about the first part of your question

 

2- For the second part: this code does the following

----- Creates these 2 DimStyles if they don't exist (they are both created with the current dimstyle as base - so you have to modify them later to your liking)

----- Checks all dimstyles for the primary units format, and stores the fractional styles in a list, and all otehrs in another lis (except the 2 mensioned styles ofcourse)

------ asks you to select the dimensions to check, and modify their stule as required..

 

Limitations:

- This command works only in the current space

- It dosent work on dimentions inside bloks or xrefs..

(defun C:DimStCH(/ CurStyleProps CurStyleName FractionalList NoneFractionalList x XElm CurDim CurDimStyle)
   (if (not (tblsearch "DIMSTYLE" "CustFract")) (command "-DIMSTYLE" "S" "CustFract") )
   (if (not (tblsearch "DIMSTYLE" "CustDec")) (command "-DIMSTYLE" "S" "CustDec") )

   (setq CurStyleProps (tblnext "DIMSTYLE" t))
   (while CurStyleProps
      (setq CurStyleName (cdr (cadr CurStyleProps))) 
      (if (and (not (equal CurStyleName "CustFract")) (not (equal NextStyleName "CustDec")))
         (if (equal (cdr (assoc '270 CurStyleProps)) 5 )
            (setq FractionalList (append FractionalList (list CurStyleName)))
            (setq NoneFractionalList (append NoneFractionalList (list CurStyleName)))
         )
      )
      (setq CurStyleProps (tblnext "DIMSTYLE"))
   )


   (while (setq X (ssnamex (ssget '((0 . "DIMENSION")) )) )	
      (foreach XElm x						 
         (if (not (listp (cadr XElm)))				
	    (progn						
		(Setq CurDim (entget (cadr XElm)) )								
                (setq CurDimStyle (cdr (assoc '3 CurDim)))
                (if (member CurDimStyle FractionalList)
		   (progn
		      (setq CurDim
		         (subst (cons '3 "CustFract")
                	      (assoc '3 CurDim) 
                	      CurDim
		         )
		      )
		   (entmod CurDim)
		   )
                   (progn
		      (setq CurDim
		         (subst (cons '3 "CustDec")
                	      (assoc '3 CurDim) 
                	      CurDim
		         )
		      )
		   (entmod CurDim)
		   )
		)
	    )
         )
      )
   )
)
0 Likes
Message 3 of 15

Anonymous
Not applicable
Thanks a ton for the help. The code creates the two standard dimension styles, but it assigns all of them to the CustDec dimension style regardless of the primary unit format. Can you offer any help with this?
0 Likes
Message 4 of 15

elshawadfy
Collaborator
Collaborator

Most likely you tried the command in a drawing that doesnn't have these two styles.. in that case, both styles were created and assigned to, they just look the same. (check the properties of two dimesions that are supposed to look different and see the style of each), . To fix that . just hit "d" space for "Dimstyle", and go to the Fractal style and change its settings to your liking you will see the difference..

 

The reason they both look the same, is that that if the drawing didn't have the styles already, the 2 newly created styles take after the current active style at the time of thir creation by efault.. I didnt't try to modify the styles to not mess them if they were alredy in the drawing, and to remind oyu to set all their settings as you need, not just what I'll preset them to..

----

Test again and tell me..

0 Likes
Message 5 of 15

Anonymous
Not applicable

Below is an image showing the Primary Unit properties for each of the two dimension styles.  CustDec has a decimal Unit Format, and CustFrac has a Fractional Unit Format.  With these already set in a drawing, I then run the code on the two dimensions in the drawing below.  One dimension has a fractional Unit Format and the other Decimal.  Each dimension has a different dimension style with names that are NOT CustDec or CustFrac.  The result of the code assigns the CustDec dimstyle to both dimensions, instead of one dimension to CustDec and the other to CustFrac.

 

It seems as if it's not assigning dimstyle according to the Unit Format (DIMLUNIT).

 

Dimstyles.jpg

 

Below is screenshot of fractional dimension properties before I run the code

Fractional Dimension Initial.jpg

Below is screenshot of same dimension after running the function

Fractional Dimension After Function.jpg

 

No matter what Unit Format the dimension has before running the code, it always changes the format to decimal.  Can you please offer help to find a solution?

 

Thanks for your help,

Candace

0 Likes
Message 6 of 15

elshawadfy
Collaborator
Collaborator

Can you perhaps upload a sample file with dimensions to change?

(Can't see the writing in your photos, but my gess, would be that the fractional dimensions were changed in the properties (overrides), but their original styles were decimal perhaps?)

0 Likes
Message 7 of 15

Anonymous
Not applicable

Attached is a dwg with two dimensions (one fractional/one decimal).

0 Likes
Message 8 of 15

elshawadfy
Collaborator
Collaborator

Try it now.. (Sorry - fractional containes stacked and not stacked, the code checked for one of them only, -- now it should work. You still need to edit the style afterwards to your prefferrence)

 

Previous notes still hold:

1- this code does the following

----- Creates these 2 DimStyles if they don't exist (they are both created with the current dimstyle as base - so you have to modify them later to your liking)

----- Checks all dimstyles for the primary units format, and stores the fractional styles in a list, and all otehrs in another lis (except the 2 mensioned styles ofcourse)

------ asks you to select the dimensions to check, and modify their style as required..

 

Limitations:

- This command works only in the current space

- It dosent work on dimentions inside bloks or xrefs..

- Temporary overrides of Dim. Props. still not included.. (the original style should be fractiona)

 

(defun C:DimStCH(/ CurStyleProps CurStyleName CurStyleFormat FractionalList NoneFractionalList x XElm CurDim CurDimStyle);
   (if (not (tblsearch "DIMSTYLE" "CustFract")) (command "-DIMSTYLE" "S" "CustFract") )
   (if (not (tblsearch "DIMSTYLE" "CustDec")) (command "-DIMSTYLE" "S" "CustDec") )

   (setq CurStyleProps (tblnext "DIMSTYLE" t))
   (while CurStyleProps
      (setq CurStyleName (cdr (cadr CurStyleProps))) 
      (setq CurStyleFormat (cdr (assoc '270 CurStyleProps)))
      (if (and (not (equal CurStyleName "CustFract")) (not (equal CurStyleName "CustDec")))
         (if (member CurStyleFormat '(5 7) )
            (setq FractionalList (append FractionalList (list CurStyleName)))
            (setq NoneFractionalList (append NoneFractionalList (list CurStyleName)))
         )
      )
      (setq CurStyleProps (tblnext "DIMSTYLE"))
   )


   (while (setq X (ssnamex (ssget '((0 . "DIMENSION")) )) )	
      (foreach XElm x						 
         (if (not (listp (cadr XElm)))				
	    (progn						
		(Setq CurDim (entget (cadr XElm)) )								
                (setq CurDimStyle (cdr (assoc '3 CurDim)))
                (if (member CurDimStyle FractionalList)
		   (progn
		      (setq CurDim
		         (subst (cons '3 "CustFract")
                	      (assoc '3 CurDim) 
                	      CurDim
		         )
		      )
		   (entmod CurDim)
		   )
                   (progn
		      (setq CurDim
		         (subst (cons '3 "CustDec")
                	      (assoc '3 CurDim) 
                	      CurDim
		         )
		      )
		   (entmod CurDim)
		   )
		)
	    )
         )
      )
   )
)

 

 

Message 9 of 15

Anonymous
Not applicable

This program is great.  You've helped me out greatly.  I tried to alter a line of the code so that all dimensions on the current layout are automatically selected without having to manually do so.

 

I changed:

(while (setq X (ssnamex (ssget '((0 . "DIMENSION")) )) )

 

to

(while (setq X (ssnamex(ssget "X" (list(cons 0 "DIMENSION"))) )

 

and I also tried

(while (setq X (ssnamex (ssget "X" '((0 . "DIMENSION")) )) )

 

with no success.  The program either freezes or runs so slowly that I have to end it through task manager.  Are either of the lines above valid?

 

Thanks,

Candace

0 Likes
Message 10 of 15

elshawadfy
Collaborator
Collaborator
Accepted solution

Glad you foun it useful @Anonymous 🙂

 

As for your qustion, you did well, you just ddn't take into consideration that the select was within a loop (while) to keep working until you stop seleecting.. After your modification it kept going on selecting all dimensions each time without ever stopping..

 

Anyway, this is the new code after somw maintenance and after adding a couple of options.. Enjoy Smiley Happy

 

Lisp file is attatched below..

 

;;; 
;;; 
;;; 
;;; 
;;; This file was created by El-Shawadify upon babijay43 reqeust in Autodesk Community Forum
;;; Thread link: http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/unassign-material-from-active-part-and-change-dimstyle-using/m-p/6350706
;;; May 27th, 2016
;;;
;;; The command changes the DimStyle of selected (or all) dimensions depending on their the unit format of their original style: ..
;;; --- All dimensions that their original stye uses "Fractional" unit format, thir DimStyle becomes "CustFract"
;;; --- All dimensions that their original stye uses "Decimal" unit format, thir DimStyle becomes "CustDec" 
;;; You still need to customize the new DimStyles mentioned [CustFract, CustDec] the way you want..
;;; 
;;; You may modify this file as long as you keep this header at the biginning..
;;; 
;;; 
;;; 
;
;====================================================================== Main Defun
;
(defun C:DSCH(/ CurStyleProps CurStyleName CurStyleFormat 
		   FractionalList NoneFractionalList 
                   x XElm CurDim CurDimStyle 
                   *error* msg
                   OriginalErr Err1 Err2
		   DimStyleMod
                   SelectMode
                ); 
;
;====================================================================== Error Fuctions
;
(setvar "CMDECHO" 0) 
(setq OriginalErr *error*)

(defun Err1 ( DimModErr)
   (command-s "undo" "end")
   (setvar "CMDECHO" 1)
   (princ (strcat "\nError: " DimModErr " The command was interrupted while changing the DimStyles of selected dimensions ::: Beware - Not all dimension styles were changed.."))
   (setq *error* OriginalErr)
);

(defun Err2 ( DimModErr)
   (command-s "undo" "end")
   (setvar "CMDECHO" 1)
   (princ (strcat DimModErr "\n Command Ended Safely.. No new dimensions to modify"))
   (setq *error* OriginalErr)
)
(Command "Undo" "Begin")
(setq *error* Err1)
;
;====================================================================== Main Function Def: DimStyleMod
;
(Defun DimStyleMod (x) 
   (setq *error* Err1) 
   (foreach XElm x						 
      (if (not (listp (cadr XElm)))				
         (progn						
            (Setq CurDim (entget (cadr XElm)) )								
            (setq CurDimStyle (cdr (assoc '3 CurDim)))
            (if (member CurDimStyle FractionalList)
	       (progn
                  (setq CurDim
                     (subst (cons '3 "CustFract")
                	    (assoc '3 CurDim) 
                	    CurDim
		     )
		     )
		   (entmod CurDim)
		)
               (progn
                  (setq CurDim
                     (subst (cons '3 "CustDec")
                	    (assoc '3 CurDim) 
                	    CurDim
		     )
		  )
		  (entmod CurDim)
	       )
            )
	 )
      )
   )
   (setq *error* Err1)
)
;
;====================================================================== Command Main Body
;

(if (not (tblsearch "DIMSTYLE" "CustFract")) (command "-DIMSTYLE" "S" "CustFract") )
(if (not (tblsearch "DIMSTYLE" "CustDec")) (command "-DIMSTYLE" "S" "CustDec") )

(setq CurStyleProps (tblnext "DIMSTYLE" t))
(while CurStyleProps
   (setq CurStyleName (cdr (cadr CurStyleProps))) 
   (setq CurStyleFormat (cdr (assoc '270 CurStyleProps)))
   (if (and (not (equal CurStyleName "CustFract")) (not (equal CurStyleName "CustDec")))
      (if (member CurStyleFormat '(5 7) )
         (setq FractionalList (append FractionalList (list CurStyleName)))
         (setq NoneFractionalList (append NoneFractionalList (list CurStyleName)))
      )
   )
   (setq CurStyleProps (tblnext "DIMSTYLE"))
)

(initget "All ExcludeFrozen ExcludeLocked ExcludeBoth Select")
(setq SelectMode (getkword "\nDimensions to modify: [All/ExcludeFrozen/ExcludeLocked/ExcludeBoth/Select] <Select>: "))
(if (not SelectMode) (setq SelectMode "Select"))
(princ SelectMode )

(cond
   ( (= SelectMode "All")
      (progn
         (setq X (ssnamex (ssget "_X" '((0 . "DIMENSION")) )) )
         (DimStyleMod x)
      )
   )
   ;;;
   ( (= SelectMode "ExcludeFrozen")
      (progn
         (setq X (ssnamex (ssget "_A" '((0 . "DIMENSION")) )) )
         (DimStyleMod x)
      )
   )
   ;;;
   ( (= SelectMode "ExcludeLocked")
      (progn
         (setq X (ssnamex (ssget "_X:L" '((0 . "DIMENSION")) )) )
         (DimStyleMod x)
      )
   )
   ;;;
   ( (= SelectMode "ExcludeBoth")
      (progn
         (setq X (ssnamex (ssget "_A:L" '((0 . "DIMENSION")) )) )
         (DimStyleMod x)
      )
   )
   ;;;
   ( (= SelectMode "Select")
      (while (setq X (ssnamex (ssget '((0 . "DIMENSION")) )) )	
         (DimStyleMod x)
      )
   )
)


(Command "Undo" "End")
(setq *error* OriginalError)
(Setvar "CMDECHO" 1)
(princ)


)

 

 

Message 11 of 15

Anonymous
Not applicable
This is above and beyond! I am very thankful for your help with this and saving me from the confusion of LISP programming. Thank you thank you thank you. The program is amazing.
0 Likes
Message 12 of 15

elshawadfy
Collaborator
Collaborator

You're welcome @Anonymous !! Glad it helped Smiley Happy Smiley Happy

 

 

0 Likes
Message 13 of 15

Anonymous
Not applicable
When I run the program twice, the first time it correctly assigns the dimension to a dimstyle but any time after that it sets all dimensions to the CustDec dimstyle regardless of fractional format. Any thoughts?

Thanks,
Candace
0 Likes
Message 14 of 15

Anonymous
Not applicable
Ignore the last post. I was able to run the program multiple time on the same drawing by removing this section of code:

;(if (and (not (equal CurStyleName "CustFract")) (not (equal CurStyleName "CustDec")))

to allow the two standard names shown in the line above to be written to the FractionalList and NoneFractionalList for comparison when I run the program the second time.

Thanks again for all of your help!
0 Likes
Message 15 of 15

elshawadfy
Collaborator
Collaborator

@Anonymous wrote:
Ignore the last post. I was able to run the program multiple time on the same drawing by removing this section of code:

;(if (and (not (equal CurStyleName "CustFract")) (not (equal CurStyleName "CustDec")))

to allow the two standard names shown in the line above to be written to the FractionalList and NoneFractionalList for comparison when I run the program the second time.

Thanks again for all of your help!

I would advice against the removing the line for 2 reasons:

- The software will endup repeating it'd work, and in a big drawing it will take a long time without producing something new..

- If you ran the command the secon time before customizing the 2 new styles, since they will be of the same type (like the one active the first time you ran the command - whether it's decimal or fractional) you will end up having one style either CustomeFract or CustDec.. 

 

I fixed the code by adding a second check within the main function "DimStyleMod": 

 

(if (and (not (equal CurDimStyle "CustFract")) (not (equal CurDimStyle "CustDec")))

The final form of the code:

 

;;; 
;;; 
;;; 
;;; 
;;; This file was created by El-Shawadify upon babijay43 reqeust in Autodesk Community Forum
;;; Thread link: http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/unassign-material-from-active-part-and-change-dimstyle-using/m-p/6350706
;;; May 27th, 2016
;;;
;;; The command changes the DimStyle of selected (or all) dimensions depending on their the unit format of their original style: ..
;;; --- All dimensions that their original stye uses "Fractional" unit format, their DimStyle becomes "CustFract"
;;; --- All dimensions that their original stye uses "Decimal" unit format, their DimStyle becomes "CustDec" 
;;; You still need to customize the new DimStyles mentioned [CustFract, CustDec] the way you want..
;;; 
;;; You may modify this file as long as you keep this header at the biginning..
;;; 
;;; 
;;; 
;
;====================================================================== Main Defun
;
(defun C:DSCH(/ CurStyleProps CurStyleName CurStyleFormat 
		   FractionalList NoneFractionalList 
                   x XElm CurDim CurDimStyle 
                   *error* msg
                   OriginalErr Err1 Err2
		   DimStyleMod
                   SelectMode
                ); 
;
;====================================================================== Error Fuctions
;
(setvar "CMDECHO" 0) 
(setq OriginalErr *error*)

(defun Err1 ( DimModErr)
   (command-s "undo" "end")
   (setvar "CMDECHO" 1)
   (princ (strcat "\nError: " DimModErr " The command was interrupted while changing the DimStyles of selected dimensions ::: Beware - Not all dimension styles were changed.."))
   (setq *error* OriginalErr)
);

(defun Err2 ( DimModErr)
   (command-s "undo" "end")
   (setvar "CMDECHO" 1)
   (princ (strcat DimModErr "\n Command Ended Safely.. No new dimensions to modify"))
   (setq *error* OriginalErr)
)
(Command "Undo" "Begin")
(setq *error* Err1)
;
;====================================================================== Main Function Def: DimStyleMod
;
(Defun DimStyleMod (x) 
   (setq *error* Err1) 
   (foreach XElm x						 
      (if (not (listp (cadr XElm)))				
         (progn						
            (Setq CurDim (entget (cadr XElm)) )								
            (setq CurDimStyle (cdr (assoc '3 CurDim)))
            (if (and (not (equal CurDimStyle "CustFract")) (not (equal CurDimStyle "CustDec")))
               (if (member CurDimStyle FractionalList)
	          (progn
                     (setq CurDim
                        (subst (cons '3 "CustFract")
                           (assoc '3 CurDim) 
                	   CurDim
		        )
		        )
		      (entmod CurDim)
		   )
                  (progn
                     (setq CurDim
                        (subst (cons '3 "CustDec")
                               (assoc '3 CurDim) 
                               CurDim
		        )
		     )
	             (entmod CurDim)
	          )
               )
            )
	 )
      )
   )
   (setq *error* Err1)
)
;
;====================================================================== Command Main Body
;

(if (not (tblsearch "DIMSTYLE" "CustFract")) (command "-DIMSTYLE" "S" "CustFract") )
(if (not (tblsearch "DIMSTYLE" "CustDec")) (command "-DIMSTYLE" "S" "CustDec") )

(setq CurStyleProps (tblnext "DIMSTYLE" t))
(while CurStyleProps
   (setq CurStyleName (cdr (cadr CurStyleProps))) 
   (setq CurStyleFormat (cdr (assoc '270 CurStyleProps)))
   (if (and (not (equal CurStyleName "CustFract")) (not (equal CurStyleName "CustDec")))
      (if (member CurStyleFormat '(5 7) )
         (setq FractionalList (append FractionalList (list CurStyleName)))
         (setq NoneFractionalList (append NoneFractionalList (list CurStyleName)))
      )
   )
   (setq CurStyleProps (tblnext "DIMSTYLE"))
)

(initget "All ExcludeFrozen ExcludeLocked ExcludeBoth Select")
(setq SelectMode (getkword "\nDimensions to modify: [All/ExcludeFrozen/ExcludeLocked/ExcludeBoth/Select] <Select>: "))
(if (not SelectMode) (setq SelectMode "Select"))
(princ SelectMode )

(cond
   ( (= SelectMode "All")
      (progn
         (setq X (ssnamex (ssget "_X" '((0 . "DIMENSION")) )) )
         (DimStyleMod x)
      )
   )
   ;;;
   ( (= SelectMode "ExcludeFrozen")
      (progn
         (setq X (ssnamex (ssget "_A" '((0 . "DIMENSION")) )) )
         (DimStyleMod x)
      )
   )
   ;;;
   ( (= SelectMode "ExcludeLocked")
      (progn
         (setq X (ssnamex (ssget "_X:L" '((0 . "DIMENSION")) )) )
         (DimStyleMod x)
      )
   )
   ;;;
   ( (= SelectMode "ExcludeBoth")
      (progn
         (setq X (ssnamex (ssget "_A:L" '((0 . "DIMENSION")) )) )
         (DimStyleMod x)
      )
   )
   ;;;
   ( (= SelectMode "Select")
      (while (setq X (ssnamex (ssget '((0 . "DIMENSION")) )) )	
         (DimStyleMod x)
      )
   )
)


(Command "Undo" "End")
(setq *error* OriginalError)
(Setvar "CMDECHO" 1)
(princ)


)

(And - if possible - could you unmark the earlier answer and mark this one to avoid peaole having problems with the code?)

Sorry for the inconvinience ..

 

Hope it works fine now.. Smiley Happy

 

 

LispFIle is attatched..

 

0 Likes