Jump to content

Recommended Posts

Posted

I want to change the color property in selected entities, but increasing in each cycle the corresponding color value.

What is wrong in this code?:oops:

 

(defun c:CCN (/ cn inc a n i b1 c d b2)
(setvar "cmdecho" 0)
(setq cn (getint "\nDefine Color Number"))
(setq inc (getint "\nDefine Color Number Increment"))
(setq a (ssget))
(setq n (sslength a))
(setq i 0)
(repeat n
       (setq b1 (entget (ssname a i)))
       (setq i (1+ i))
       (setq c (assoc 62 b1))
       (setq d (cons (car c) cn))
       (setq b2 (subst d c b1))
       (entmod b2)
	(setq cn (+ cn inc))
)
(PRINC)
)
(prompt "\nType: CCN")

Posted

Please don't forget that when the color is set as ByLayer, the DXF code 62 isn't present, therefore cannot use SUBST, rather need to append the color dotted pair.

(defun c:CCN ( / cn inc a n i b1 c d b2)
(setvar "cmdecho" 0)
(setq cn (getint "\nDefine Color Number[color=red]:[/color] "))
(setq inc (getint "\nDefine Color Number Increment[color=red]: [/color]"))
(setq a (ssget))
(setq n (sslength a))
(setq i 0)
(repeat n
       (setq b1 (entget (ssname a i)))
       (setq i (1+ i))
       (setq d (cons [color=red]62[/color] cn))
       [color=red](if[/color] (setq c (assoc 62 b1))
        (setq b2 (subst d c b1))
        [color=red](setq b2 (cons d b1))[/color]
       [color=red])[/color]
       (entmod b2)
       (setq cn (+ cn inc))
)
(PRINC)
)
(prompt "\nType: CCN")

May want also to add a protection to don't exceed 256 as color index.

Posted

Mircea,

Tanks for help.

Posted

Here are my comments / recommendations:

 

([color=BLUE]defun[/color] c:CCN ( [color=BLUE]/[/color] ci cn dx en in ss )

   [color=GREEN];; (setvar "cmdecho" 0)[/color]
   [color=GREEN];; No need to alter CMDECHO, no commands are being used[/color]

   ([color=BLUE]if[/color] [color=GREEN];; If the following returns non-nil[/color]
       ([color=BLUE]and[/color] [color=GREEN];; If all of the following expressions return non-nil[/color]
           ([color=BLUE]setq[/color] cn ([color=BLUE]acad_colordlg[/color] 1 [color=BLUE]nil[/color])) [color=GREEN];; acad_colordlg seems more suitable for this prompt[/color]
           ([color=BLUE]setq[/color] ci ([color=BLUE]getint[/color] [color=MAROON]"\nColour Increment: "[/color])) [color=GREEN];; Prompt for Colour Increment[/color]
           ([color=BLUE]setq[/color] ss ([color=BLUE]ssget[/color] [color=MAROON]"_:L"[/color])) [color=GREEN];; Using "_:L" mode string to exclude objects on locked layers[/color]
       ) [color=GREEN];; End AND[/color]
       ([color=BLUE]progn[/color] [color=GREEN];; Wrap the following into a single expression[/color]
           ([color=BLUE]setq[/color] in 0) [color=GREEN];; Initialise the counter[/color]
           ([color=BLUE]repeat[/color] ([color=BLUE]sslength[/color] ss) [color=GREEN];; Repeat a number of times equal to the number of objects in the set[/color]
               ([color=BLUE]setq[/color] en ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] ss in))) [color=GREEN];; Retrieve the DXF data of the entity at index 'in' in the set[/color]
               ([color=BLUE]if[/color] ([color=BLUE]setq[/color] dx ([color=BLUE]assoc[/color] 62 en)) [color=GREEN];; If DXF 62 is present[/color]
                   ([color=BLUE]setq[/color] en ([color=BLUE]subst[/color] ([color=BLUE]cons[/color] 62 cn) dx en)) [color=GREEN];; Update the value of DXF 62[/color]
                   ([color=BLUE]setq[/color] en ([color=BLUE]append[/color] en ([color=BLUE]list[/color] ([color=BLUE]cons[/color] 62 cn)))) [color=GREEN];; Otherwise add DXF 62[/color]
               ) [color=GREEN];; End IF[/color]
               ([color=BLUE]entmod[/color] en) [color=GREEN];; Update the drawing database[/color]
               ([color=BLUE]setq[/color] cn ([color=BLUE]+[/color] cn ci) [color=GREEN];; Increment the colour[/color]
                     in ([color=BLUE]1+[/color] in) [color=GREEN];; Increment the counter[/color]
               ) [color=GREEN];; End SETQ[/color]
               ([color=BLUE]if[/color] ([color=BLUE]<[/color] 255 cn) ([color=BLUE]setq[/color] cn 1)) [color=GREEN];; Ensure the colour doesn't exceed 255[/color]
           ) [color=GREEN];; End REPEAT[/color]
       ) [color=GREEN];; End PROGN[/color]
   ) [color=GREEN];; End IF[/color]
   ([color=BLUE]princ[/color]) [color=GREEN];; Exit Cleanly[/color]
) [color=GREEN];; End DEFUN[/color]

Posted

Lee Mac,

Thanks for the lesson.

The comments to each line of code is a practical way to help understand and learn.:)

Posted

I need more help:

 

In this routine, if I want to set a particular value of the z coordinate as a condition for changing the color property, which the code should be added? For example, in the selection would be changed only entities which Z coordinates are situated between 5 and 15?

Posted

What type of entities are those? Since the Z coordinate may be stored under different DXF codes. For example, for lines is stored directly in end points (codes 10 and 11) while for polylines is stored separately (code 38).

Posted
What type of entities are those? Since the Z coordinate may be stored under different DXF codes. For example, for lines is stored directly in end points (codes 10 and 11) while for polylines is stored separately (code 38).

 

Ah, Ok. So in this particular case, we consider the line entities.

Of course, if a solution of polyvalent way to consider polylinhas and even splines would be excellent.

In fact, I intend to use the following routine to change the color property in the contours of a topographic survey, assigning different colors depending on their elevation.

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