Jump to content

AutoLISP: Help for beginner


luiscarneirorm

Recommended Posts

Lee Mac, or someone else, can you help me to change this function to select all entities with linetipe are not continuous? And as in this case, I also have some entities with linetipe bylayer, and others with individual linetype.

 

Here is a quick modification:

 

(defun _selectlinetype ( lt / df ls )

   (while (setq df (tblnext "LAYER" (null df)))
       (if (eq lt (cdr (assoc 6 df)))
           (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
       )
   )
   (sssetfirst nil
       (ssget "_X"
           (cons
               (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
               (if ls
                   (list
                       (cons -4 "<OR")
                       (cons 6 lt)
                       (cons 8 (apply 'strcat (cdr ls)))
                       (cons -4 "OR>")
                   )
                   (list (cons 6 lt))
               )
           )
       )
   )
)

 

To be called with the name of the linetype, e.g:

 

(_SelectLinetype "HIDDEN")

 

I shall leave the task of adding a user prompt (if necessary) to you.

 

A reference for the DXF group codes can be found here.

Link to comment
Share on other sites

  • Replies 65
  • Created
  • Last Reply

Top Posters In This Topic

  • luiscarneirorm

    33

  • BlackBox

    12

  • Lee Mac

    11

  • Tharwat

    5

Top Posters In This Topic

Posted Images

P.S.: RenderMan, thanks for your post, but I think I'll leave that for later (it's hard for me, for now:cry:)

 

You're welcome, luiscarneirorm. :)

 

I'm sorry that my post was not able to help you more.

Link to comment
Share on other sites

Here is a quick modification:

 

(defun _selectlinetype ( lt / df ls )

   (while (setq df (tblnext "LAYER" (null df)))
       (if (eq lt (cdr (assoc 6 df)))
           (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
       )
   )
   (sssetfirst nil
       (ssget "_X"
           (cons
               (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
               (if ls
                   (list
                       (cons -4 "<OR")
                       (cons 6 lt)
                       (cons 8 (apply 'strcat (cdr ls)))
                       (cons -4 "OR>")
                   )
                   (list (cons 6 lt))
               )
           )
       )
   )
)

To be called with the name of the linetype, e.g:

 

(_SelectLinetype "HIDDEN")

I shall leave the task of adding a user prompt (if necessary) to you.

 

A reference for the DXF group codes can be found here.

 

I want to select all entities with linetype not continuous.

 

I tried to change this way:

(...)
(if ([b]not[/b] eq lt (cdr (assoc 6 df)))
(...)

but as I feared is not so simple

Link to comment
Share on other sites

I want to select all entities with linetype not continuous.

 

I tried to change this way:

(...)
(if ([b]not[/b] eq lt (cdr (assoc 6 df)))
(...)

but as I feared is not so simple

 

Close, but there are a few extra parts to add:

 

(defun _selectifnotlinetype ( lt / df ls )

   [color=red](setq lt (strcase lt))[/color] [color=green];; To remove case-sensitivity[/color]

   (while (setq df (tblnext "LAYER" (null df)))
       (if [color=red](not[/color] (eq lt [color=red](strcase[/color] (cdr (assoc 6 df))[color=red])[/color])[color=red])[/color]
           (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
       )
   )
   (sssetfirst nil
       (ssget "_X"
           (cons
               (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
               (if ls
                   (list
                       (cons -4 "<OR")
                       (cons 6 [color=red](strcat "~" [color=black]lt[/color])[/color])
                       (cons 8 (apply 'strcat (cdr ls)))
                       (cons -4 "OR>")
                   )
                   (list (cons 6 [color=red](strcat "~"[/color] lt[color=red])[/color]))
               )
           )
       )
   )
)

(_selectifnotlinetype "Continuous")

Link to comment
Share on other sites

Perhaps, replacing:

 

(cons 6 lt)

... With this:

 

(cons 6 (strcat "~" lt))

:unsure:

 

this way also selects the entities that are "bylayer (continuous)" :(

 

And i have exactly the same problem with your code Lee Mac

Link to comment
Share on other sites

Lee, i change your code to:

(defun _selectifnotlinetype ( lt / df ls )

   (setq lt (strcase lt)) ;; To remove case-sensitivity

   (while (setq df (tblnext "LAYER" (null df)))
       (if (not (eq lt (strcase (cdr (assoc 6 df)))))
           (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
       )
   )
   (sssetfirst nil
       (ssget "_X"
           (cons
               (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
               (if ls
                   (list
                       (cons -4 "<OR")
                       (cons 6 (strcat  lt))
                       (cons 8 (apply 'strcat (cdr ls)))
                       (cons -4 "OR>")
                   )
                   (list (cons 6 (strcat  lt)))
               )
           )
       )
   )
)

and the result is already closer to what I meant, but still selects some entities are on layer with linetype dashed, but have the individual linetype defined as Continuous.

 

I did just the opposite of what RenderMan tell me before: i replace

(cons 6 (strcat "~" lt))

with

(cons 6 lt)

:unsure:

 

P.S: I just now detected, but the function of post 22, happens the same thing, if the entities have a lineweight set individually, and not by layer, it is not selected:(:(

Link to comment
Share on other sites

P.S: I just now detected, but the function of post 22, happens the same thing, if the entities have a lineweight set individually, and not by layer, it is not selected:(:(

 

Good catch, I had overlooked that :oops:

 

Try this instead:

 

(defun _selectifnotlinetype ( lt / df ls )

   (setq lt (strcase lt)) ;; To remove case-sensitivity

   (while (setq df (tblnext "LAYER" (null df)))
       (if (not (eq lt (strcase (cdr (assoc 6 df)))))
           (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
       )
   )
   (sssetfirst nil
       (ssget "_X"
           (cons
               (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
               (if ls
                   (list
                       (cons -4 "<OR")
                           (cons -4 "<AND")
                               (cons 6 "ByLayer")
                               (cons 8 (apply 'strcat (cdr ls)))
                           (cons -4 "AND>")
                           (cons -4 "<NOT")
                               (cons 6 (strcat lt ",ByLayer"))
                           (cons -4 "NOT>")
                       (cons -4 "OR>")
                   )
                   (list
                       (cons -4 "<NOT")
                           (cons 6 (strcat lt ",ByLayer"))
                       (cons -4 "NOT>")
                   )
               )
           )
       )
   )
)

(_selectifnotlinetype "Continuous")

 

To correct post#22:

 

(defun c:test ( / df ls lw )
   (while
       (and (setq lw (getreal "\nSpecify LWeight: "))
           (or
               (< lw  0.0)
               (< 2.11 lw)
           )
       )
       (princ "\nLineweight must be 0.0 <= x <= 2.11")
   )

   (if lw
       (progn
           (setq lw (fix (* 100 lw)))
           (while (setq df (tblnext "LAYER" (null df)))
               (if (= lw (cdr (assoc 370 (entget (tblobjname "LAYER" (cdr (assoc 2 df)))))))
                   (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
               )
           )
           (sssetfirst nil
               (ssget "_X"
                   (cons
                       (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
                       (if ls
                           (list
                               (cons -4 "<OR")
                                   (cons -4 "<AND")
                                       (cons 370 -1)
                                       (cons 8 (apply 'strcat (cdr ls)))
                                   (cons -4 "AND>")
                                   (cons 370 lw)
                               (cons -4 "OR>")
                           )
                           (list (cons 370 lw))
                       )
                   )
               )
           )
       )
   )
)

 

These ssget filter lists can be tricky at times...

Link to comment
Share on other sites

Hi.

i try to insert this code in the end off this function, to put the selected entities in the current layer.

(defun c:espessura ( / df ls lw [color=red]e[/color] )
   (while
       (and (setq lw (getreal "\nSpecify LWeight: "))
           (or
               (< lw  0.0)
               (< 2.11 lw)
           )
       )
       (princ "\nLineweight must be 0.0 <= x <= 2.11")
   )

   (if lw
       (progn
           (setq lw (fix (* 100 lw)))
           (while (setq df (tblnext "LAYER" (null df)))
               (if (<= lw (cdr (assoc 370 (entget (tblobjname "LAYER" (cdr (assoc 2 df)))))))
                   (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
               )
           )
           (sssetfirst nil
               (ssget "_X"
                   (cons
                       (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
                       (if ls
                           (list
                               (cons -4 "<OR")
                                   (cons -4 "<AND")
                                       (cons 370 -1)
                                       (cons 8 (apply 'strcat (cdr ls)))
                                   (cons -4 "AND>")
                                   (cons 370 lw)
                               (cons -4 "OR>")
                           )
                           (list (cons 370 lw))
                       )                                    
                   )
               )
           )
       )
   )
   [color=red](setq e (getvar "clayer"))        
        (command "change" ls "" "prop" "layer" e "")[/color]
)

but as I feared I did not succeed:( (the code in red is what I added)

 

Command: espessura
Specify LWeight: 0.4
change
Select objects:
Command: ESPESSURA Unknown command "ESPESSURA".  Press F1 for help.
Command: prop Unknown command "PROP".  Press F1 for help.
Command: layer
Current layer:  "0"
Enter an option 
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck
/Unlock/stAte/Description/rEconcile]: 0
Invalid option keyword.
; error: Function cancelled

what am I doing wrong? :unsure:

 

P.S: Lee, this is your function, I just changed it's name

Edited by luiscarneirorm
Link to comment
Share on other sites

what am I doing wrong? :unsure:

 

The variable 'ls' stores a list of strings, not a selection set as required by the 'chprop' command.

 

I use the sssetfirst function to highlight the selection set returned by ssget, but if instead you wanted to use this selection set in the code, it would be easier to bound it to a variable, e.g.:

 

(defun c:espessura ( / df en i ls lw ss )
   (while
       (and (setq lw (getreal "\nSpecify LWeight: "))
           (or
               (< lw  0.0)
               (< 2.11 lw)
           )
       )
       (princ "\nLineweight must be 0.0 <= x <= 2.11")
   )

   (if
       (and lw
           (progn
               (setq lw (fix (* 100 lw)))
               (while (setq df (tblnext "LAYER" (null df)))
                   (if (<= lw (cdr (assoc 370 (entget (tblobjname "LAYER" (cdr (assoc 2 df)))))))
                       (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
                   )
               )
               (setq ss
                   (ssget "_X"
                       (cons
                           (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
                           (if ls
                               (list
                                   (cons -4 "<OR")
                                       (cons -4 "<AND")
                                           (cons 370 -1)
                                           (cons 8 (apply 'strcat (cdr ls)))
                                       (cons -4 "AND>")
                                       (cons 370 lw)
                                   (cons -4 "OR>")
                               )
                               (list (cons 370 lw))
                           )
                       )
                   )
               )
           )
       )
       (repeat (setq i (sslength ss))
           (setq en (entget (ssname ss (setq i (1- i)))))
           (entmod (subst (cons 8 (getvar 'CLAYER)) (assoc 8 en) en))
       )
   )
   (princ)
)

In this way, I iterate through the Selection Set bound to the variable 'ss' using the repeat loop and modify the layer of each entity in the set.

 

P.S: Lee, this is your function, I just changed it's name

 

Not a problem :)

Link to comment
Share on other sites

Sorry, I try but can not solve by yourself.

 

The functions works, but as I said before, if the entity is in a layer with lineweight less than 'lw' and this entity have your lineweight set individually greater than 'lw' it should be selected, but that does not happens:(

 

But if on the contrary, if the entity is in a layer with lineweight greater than 'lw' and this entity have your lineweight set individually less than 'lw' it is not selected, the funccion works correctly in this case.

 

Sorry if my explanation is too confusing :oops:

Link to comment
Share on other sites

The functions works, but as I said before, if the entity is in a layer with lineweight less than 'lw' and this entity have your lineweight set individually greater than 'lw' it should be selected, but that does not happens:(

 

So you want to alter all entities with Lineweight less than the specified lineweight? I didn't notice your modification to this effect :oops:

Link to comment
Share on other sites

Try this modification:

 

(defun c:espessura ( / df en i ls lw ss )
   (while
       (and (setq lw (getreal "\nSpecify LWeight: "))
           (or
               (< lw  0.0)
               (< 2.11 lw)
           )
       )
       (princ "\nLineweight must be 0.0 <= x <= 2.11")
   )

   (if
       (and lw
           (progn
               (setq lw (fix (* 100 lw)))
               (while (setq df (tblnext "LAYER" (null df)))
                   (if (<= lw (cdr (assoc 370 (entget (tblobjname "LAYER" (cdr (assoc 2 df)))))))
                       (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
                   )
               )
               (setq ss
                   (ssget "_X"
                       (cons
                           (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
                           (if ls
                               (list
                                   (cons -4 "<OR")
                                       (cons -4 "<AND")
                                           (cons 370 -1)
                                           (cons 8 (apply 'strcat (cdr ls)))
                                       (cons -4 "AND>")
                                       (cons -4 "<=")
                                       (cons 370 lw)
                                   (cons -4 "OR>")
                               )
                               (list
                                   (cons -4 "<=")
                                   (cons 370 lw)
                               )
                           )
                       )
                   )
               )
           )
       )
       (repeat (setq i (sslength ss))
           (setq en (entget (ssname ss (setq i (1- i)))))
           (entmod (subst (cons 8 (getvar 'CLAYER)) (assoc 8 en) en))
       )
   )
   (princ)
)

Link to comment
Share on other sites

Try this modification:

 

(defun c:espessura ( / df en i ls lw ss )
   (while
       (and (setq lw (getreal "\nSpecify LWeight: "))
           (or
               (< lw  0.0)
               (< 2.11 lw)
           )
       )
       (princ "\nLineweight must be 0.0 <= x <= 2.11")
   )

   (if
       (and lw
           (progn
               (setq lw (fix (* 100 lw)))
               (while (setq df (tblnext "LAYER" (null df)))
                   (if (<= lw (cdr (assoc 370 (entget (tblobjname "LAYER" (cdr (assoc 2 df)))))))
                       (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
                   )
               )
               (setq ss
                   (ssget "_X"
                       (cons
                           (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
                           (if ls
                               (list
                                   (cons -4 "<OR")
                                       (cons -4 "<AND")
                                           (cons 370 -1)
                                           (cons 8 (apply 'strcat (cdr ls)))
                                       (cons -4 "AND>")
                                       (cons -4 "[color=black]<[/color]=")
                                       (cons 370 lw)
                                   (cons -4 "OR>")
                               )
                               (list
                                   (cons -4 "[color=black]<[/color]=")
                                   (cons 370 lw)
                               )
                           )
                       )
                   )
               )
           )
       )
       (repeat (setq i (sslength ss))
           (setq en (entget (ssname ss (setq i (1- i)))))
           (entmod (subst (cons 8 (getvar 'CLAYER)) (assoc 8 en) en))
       )
   )
   (princ)
)

 

I finally able to solve a problem alone :celebrate:

 

Lee Mac

 

 

 

So you want to alter all entities with Lineweight
less
than the specified lineweight?

i want to alter all entities with Lineweight greater than the specified lineweight.

 

I just had to reverse 2

 (defun c:espessura ( / df en i ls lw ss )
   (while
       (and (setq lw (getreal "\nSpecify LWeight: "))
           (or
               (< lw  0.0)
               (< 2.11 lw)
           )
       )
       (princ "\nLineweight must be 0.0 <= x <= 2.11")
   )

   (if
       (and lw
           (progn
               (setq lw (fix (* 100 lw)))
               (while (setq df (tblnext "LAYER" (null df)))
                   (if (<= lw (cdr (assoc 370 (entget (tblobjname "LAYER" (cdr (assoc 2 df)))))))
                       (setq ls (cons "," (cons (cdr (assoc 2 df)) ls)))
                   )
               )
               (setq ss
                   (ssget "_X"
                       (cons
                           (cons 410 (if (= 1 (getvar 'CVPORT)) (getvar 'CTAB) "Model"))
                           (if ls
                               (list
                                   (cons -4 "<OR")
                                       (cons -4 "<AND")
                                           (cons 370 -1)
                                           (cons 8 (apply 'strcat (cdr ls)))
                                       (cons -4 "AND>")
                                       (cons -4 "[color=red][b]>[/b][/color]=")
                                       (cons 370 lw)
                                   (cons -4 "OR>")
                               )
                               (list
                                   (cons -4 "[color=red][b]>[/b][/color]=")
                                   (cons 370 lw)
                               )
                           )
                       )
                   )
               )
           )
       )
       (repeat (setq i (sslength ss))
           (setq en (entget (ssname ss (setq i (1- i)))))
           (entmod (subst (cons 8 (getvar 'CLAYER)) (assoc 8 en) en))
       )
   )
   (princ)
)

Link to comment
Share on other sites

Hello again...

 

I am able to implement some functions for my work, without help :shock:, but now there is a problem that I can not solve:x

 

I have a function to recreat the hatch boundaries.

(
defun C:F8 (/ sset item i)

(setq sset 
   (
   ssget "_X" '((0 . "HATCH"))) i -1
   )

(
repeat (sslength sset)
 (
 setq item (ssname sset (setq i (1+ i)))
 )
 (command "._hatchedit" item "_B" "_P" "_Y")
)
(princ)
)

If I run only the function it works correctly, but I want to run it, after another function that I explode the blocks that also contain some hatch's

 

(defun C:f7 (/ AllBlocks SolOnly )
(setvar "qaflags" 1)
(setq AllBlocks (ssget "X" (list (cons 0 "INSERT"))))
(while (/= AllBlocks nil)
 (progn
  (command "_.explode"  AllBlocks "")
  (setq AllBlocks (ssget "X" (list (cons 0 "INSERT"))))
 );progn
);while
(setvar "qaflags" 0)
(princ)
)

the problem is:

 

Command: f10
_.explode
Select objects:   1 found
Select objects:
Command: _.explode
Select objects:   1 found
Select objects:
Command: ._hatchedit
Select hatch object:
Use the Hatch Edit dialog box to modify gradients.
Select hatch object: _B
Specify first corner: _P
Invalid window specification.
; error: Function cancelled

the F10 function just call the F7 function and then the F8 function

 

 

my goal is to change the hatch boundaries to a specified layer, and if it is possible, after this, delete the hatch's and the older original boundaries :?

Link to comment
Share on other sites

I've been searching and have tried to solve this problem in several ways, but I can not do it.

 

I am suspicious that the problem stems from the F7 function, to change the variable value "qaflags"

 

I am right? how can I solve this problem?

 

Edit:

I discovered that this was not my problem

 

The problem is that after i explode the block, i get hatch's with invalid boundaries...

Edited by luiscarneirorm
Link to comment
Share on other sites

I came up with a new problem :oops:

 

poli.jpg

 

the upper triangle is defined as a polyline with

start segment with: 0

end segment with : 1.5

 

if i explode them, it becomes a simple line, but I need the outline is as below

 

it is possible to do that???

poli.dxf

Link to comment
Share on other sites

Hi.

 

Lee,i went through your site and found there just what I needed (Polyline Outline), but does not work properly, at least in my case:(

 

You (or someone else) can take a look?

 

I put the file in the previous post

 

P.S: I've been testing with several polylines, and found that it works properly only with the start segment with, but the end segment with appears to me that ignores.

Link to comment
Share on other sites

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...