Autolisp Programming

Autolisp Programming

Anonymous
Not applicable
786 Views
6 Replies
Message 1 of 7

Autolisp Programming

Anonymous
Not applicable

Hi,

 

I wrote this program to automate translating Solidworks standard to Autocad standard.

 

Solidworks drawing saved to Autocad will be something like Layer - name, Linetype - hidden, color - red.

 

Autocad typically, name, linetype bylayer, color bylayer.

 

What I trying to do is to create a loop to extract the layer names from the table, create new layers with "-HID" suffix after the layer name.

 

Find all entities with dxf code 6 and dxf code 8, change them to the new layers.  

 

Program work 90%.  It has a error warning.

 

error: bad argument type:

 

Not sure how to fix it.

 

 

 

(defun c:Fixlay (/ d r x setname layername ltype_name layer_name)
(setvar "CMDECHO" 0)
(while (setq d (tblnext "layer" (null d)))
(setq r (cons (cdr (assoc 2 d)) r))
)
(setq x 0) (while (< x 100) (setq x (+ x 1)) ;Loop
(setq setname (nth x r)) ;Extract name
(setq layername (strcat (nth x r) "-HID")) ;Extract name from layer table & add "-HID" to name
(command "._layer" "m" layername "lt" "HIDDEN" "" "c" 1 "" "") ;Made layer

(setq ltype_name (cons 6 "Hidden"))
(setq layer_name (cons 8 setname))

(setq SETD (ssget "X" (list ltype_name layer_name)))
(if (null SETD)
(princ "\nENTITIEs NOT MOVED")
(progn
(command "._CHPROP" SETD "" "LA" layername "")))
(princ "\nENTITIES MOVED")
) ;END loop
(setvar "CMDECHO" 1)
(PRINC)
)

 

0 Likes
Accepted solutions (2)
787 Views
6 Replies
Replies (6)
Message 2 of 7

hmsilva
Mentor
Mentor
Accepted solution

Hi MuddyPipe,

 

maybe something like this

 

(defun c:Fixlay (/ d r x setname layername ltype_name layer_name)
(setvar "CMDECHO" 0)
(while (setq d (tblnext "layer" (null d)))
(setq r (cons (cdr (assoc 2 d)) r))
)
  (foreach x r
;(setq x 0) (while (< x 100) (setq x (+ x 1)) ;Loop
;(setq setname (nth x r)) ;Extract name

(setq ltype_name (cons 6 "Hidden"))
(setq layer_name (cons 8 x))

(setq SETD (ssget "X" (list ltype_name layer_name)))
(if (null SETD)
(princ "\nENTITIES NOT MOVED")
(progn
(setq layername (strcat x "-HID")) ;Extract name from layer table & add "-HID" to name
(command "._layer" "m" layername "lt" "HIDDEN" "" "c" 1 "" "") ;Made layer
(command "._CHPROP" SETD "" "LA" layername "Color" "ByLayer" "LType" "ByLayer" "")
(princ "\nENTITIES MOVED")))
) ;END loop
(setvar "CMDECHO" 1)
(PRINC)
)

 

Hope that helps

Henrique

EESignature

0 Likes
Message 3 of 7

Kent1Cooper
Consultant
Consultant

What does it say after "bad argument type"?  [See my theory below.]

 

I think one thing that may not cause that error, but should sometimes cause it not to work on every Layer, is that the first time through the (while) loop, the index number x is set to 1 before it gets a Layer name from the list.  In the (nth) function, the first thing in a list is index number 0, so as you have it, it will never look at the first Layer name in the list.  Whether that gives you a result you don't expect will depend on whether there are any things on that first Layer with a Hidden linetype override.  You can either set x to -1 at first, or have it increased by 1 some time after it has pulled a Layer name from the list, so that it will still be 0 at the first try.

 

Also, it shouldn't interfere with the operation, but I think your "ENTITIES MOVED"notice should be earlier, inside the (progn) function that runs when there are things in the SETD selection set.  Where you have it, it looks like it will say that entities have been moved even if none have been [in which case I think it will say both that they haven't and that they have].

 

You don't need to overshoot the likely number of Layer names with (while (< x 100) ..., but you can have it do exactly the right number of times, using (length r) [exactly how you work it in depends on whether you start with x at -1 or 0].  I'm guessing that's the source of the error, which I'm also guessing is a  "stringp nil" error, because once it goes past the end of the list of names when there are fewer than 100, (nth x r) will return nil, so:

(setq layername (strcat (nth x r) "-HID"))

 

will not be able to add -HID to the end of that [it can't concatenate nothingness into a string with another string].

Kent Cooper, AIA
Message 4 of 7

Anonymous
Not applicable

Hi Kent & Henrique,

 

Thank you both for taking the time to fix and explaining what's wrong with my program.

 

The program works great.

 

It may take me days to figure this one out.

 

Greatly appreciated.

 

Dennis

0 Likes
Message 5 of 7

hmsilva
Mentor
Mentor
Accepted solution

@MuddyPipe wrote:

The program works great.

It may take me days to figure this one out.

Greatly appreciated.


You're welcome, Denis.

Your code, slightly modified and with some annotations...

 

(defun c:Fixlay (/ d layn r ss)
  ;; suppresses the echo
  (setvar 'CMDECHO 0)
  ;; create a list with all layer names
  (while (setq d (tblnext "layer" (null d)))
    (setq r (cons (cdr (assoc 2 d)) r))
  )
  ;; reverse layer names list to get the original order
  (setq r (reverse r))
  ;; step through the list 'r', assign each element in the list to the variable 'x'
  (foreach x r
    ;; select entities in layer 'x' with Hidden linetype
    (if (setq ss (ssget "_X" (list '(6 . "Hidden") (cons 8 x))))
      (progn;; if set variable 'ss' with a selection set
        ;; set the variable 'layn' with the original layer name plus '-HID'
        (setq layn (strcat x "-HID"))   ;Extract name from layer table & add "-HID" to name
        ;; create a layer with the name assigned to 'layn' variable, ltype 'hidden' and color 1
        (command "._layer" "Make" layn "LType" "HIDDEN" "" "Color" 1 "" "") ;Made layer
        ;; change the selected objects to the newly created layer, and set objects color and linetype to bylayer
        (command "._chprop" ss "" "LAyer" layn "Color" "ByLayer" "LType" "ByLayer" "")
        ;; print at the command line, the number of objects that have been moved...
        (princ (strcat "\n " (itoa (sslength ss)) " entities moved from layer " x " to layer " layn ". "))
      );; progn end
      ;; if variable 'ss' is not set
      ;; print at the command line, that, there are no entities to be moved...
      (princ (strcat "\n No entities to be moved in layer " x "..."))
    );; if end
  );; foreach end
  ;; restores the echo
  (setvar 'CMDECHO 1)
  ;; set the focus to the text screen, to read the report...
  (textscr)
  ;; silently exits
  (princ)
);; defun end

 

I hope this helps

Henrique

EESignature

0 Likes
Message 6 of 7

Anonymous
Not applicable
Excellent documentation.

Documentation help me understand the logic and the flow of the program.

We are moving away from AutoCAD toward Solidword.

This program will made our Solidworks translated drawing looks like AutoCAD for submitting.

Our drafters complained how they were not able to managed their drawings because Solidworks dumped everything in one layer.

Dennis

0 Likes
Message 7 of 7

hmsilva
Mentor
Mentor
Denis, I'm glad I could help.

Henrique

EESignature

0 Likes