Jump to content
Shameless1

Illustrator converted dwg - object colors to layer

Recommended Posts

Shameless1

I’m a longtime reader and cad user, yet first time poster. I have dabbled with creating list routines in the past but it’s been many years. So, it makes more sense for me to search and inquire with those skilled in the arts of lisp creation and object manipulation.

 

I know Lee Mac has done many fine routines with regard to this, but I can’t quite find what I seek.

 

I routinely receive Illustrator drawings that I must export to ACAD. In Illustrator, objects typically have a Stroke and a Fill. When converted to dwg the resulting file has many objects all on “Layer 1” (some are hatch patterns, most are splines or polylines). Some have true color values (RGB) assigned to the objects, others might be indexed, and still some are set to “by layer”. Sometimes, the resulting objects are in a block in the dwg. I usually explode the block to get to the base elements before processing.

 

I would like a routine that explodes the blocks (nested or otherwise),  takes all of the objects based on their color values and moves them to a created layer appropriately named with the value, and color based on the value. Then the objects on the layer set back to “By layer”. Additionally, Hatches to be on layer signified with Hatch in the name, and linework­ on a layer with Linework in the name. (This is for filtering purposes so I can choose to see only hatches, or only linework)

 

Example layer names:

“Hatch – (1)”,

“Linework – (1)”,

“Hatch – (237 28 38)”,

“Linework – (237 28 38)”,

 

Further, Objects with color 0,0,0 are typically white in color and should be set to 255,255,255.

Black objects 0,0,1 should be 0,0,0.

Purge at the end to remove excess unneeded items.

 

I believe a lisp routine that could do all this should be fairly straightforward to many of the lisp guru’s in here. I am also open to other suggested routines that anyone might know of to aid with processing Illustrator converted dwg’s to streamlined and efficient AutoCAD drawings.

I have attached a DWG for reference exported from Illustrator. It also includes an attached Image for reference.

Ultimately in the end, I isolate linework. Sometimes convert spline to polylines and then trim away overlapping lines to produce clean outlines and hatches without all the overlapping elements, etc.

I cannot be the only person that has sought routines for processing dwg conversions from Illustrator in the timeliest manner. I often spend a great deal of time “cleaning up” these converted files.

Thanks for any assistance.

 

 

TigerHead_1.jpg

TigerHead.dwg

Share this post


Link to post
Share on other sites
ronjonp

Here's a good start for you with some comments to help you learn. It does not explode the blocks but will do the layering. Welcome to CadTutor! 🍻

(defun c:foo (/ co d la ln lp rgb x)
  ;; RJP » 2019-08-27
  ;; Not much error checking!
  ;; Unlock all layers
  (vlax-for a (vla-get-layers (setq d (vla-get-activedocument (vlax-get-acad-object))))
    (vlax-put a 'lock 0)
  )
  ;; Iterate all drawing objects
  (vlax-for a (vla-get-blocks d)
    (vlax-for b	a
      ;; Get truecolor object
      (setq co (vla-get-truecolor b))
      ;; Get RGB values for layername and check to swap black for white etc...
      (setq rgb (mapcar '(lambda (x) (vlax-get co x)) '(red green blue)))
      ;; Check for bylayer, index color or change rgb if 0,0,0 or 0,0,1
      (setq rgb
	     (cond ((= 256 (vla-get-color b))
		    ;; Uses the index color of the layer the object is already on
		    (list (vla-get-color (vla-add (vla-get-layers d) (vla-get-layer b))))
		   )
		   ;; Index color
		   ((null (assoc 420 (entget (vlax-vla-object->ename b)))) (list (vla-get-color b)))
		   ;; Black to white
		   ((equal '(0 0 0) rgb) '(255 255 255))
		   ;; Almost black to black
		   ((equal '(0 0 1) rgb) '(0 0 0))
		   ;; Just return RGB
		   (rgb)
	     )
      )
      ;; Set RGB colors if list has 3 elements
      (and (= 3 (length rgb)) (vla-setrgb co (car rgb) (cadr rgb) (caddr rgb)))
      ;; Create layer prefix based on object type
      (setq lp (cond ((= "AcDbHatch" (vla-get-objectname b)) "Hatch")
		     ("Linework")
	       )
      )
      ;; Uncomment below to create layer prefix based on object type not grouped as "Linework"
      ;; (setq lp (substr (vla-get-objectname b) 5))
      ;; Add new layer
      (setq la (vla-add (vla-get-layers d) (setq ln (strcat lp " - " (vl-princ-to-string rgb)))))
      ;; Set layer color
      (if (= 1 (length rgb))
	(vla-put-color la (car rgb))
	(vla-put-truecolor la co)
      )
      ;; Make object color bylayer
      (vla-put-color b 256)
      ;; Move object to layer
      (vla-put-layer b ln)
    )
  )
  ;; Purge 3 times
  (repeat 3 (vla-purgeall d))
  (princ)
)

 

Edited by ronjonp
  • Thanks 1

Share this post


Link to post
Share on other sites
Shameless1

@ronjonp You are correct in that this is a good start. It does perform well, but I notice it does not take in consideration if an object color is in Indexed color mode and not true color mode.

I am hoping it would also incorporate this functionality so as to test the object for an assoc 62 value and apply that in those instances, and the true color value in those where the objects are using the assoc 420 value. Does that make sense?

 

I tried this on my sample file, by changing one of the objects to an indexed color. The routine resulted with the object on a layer with the rgb value.

 

Again, I struggle with trying to incorporate such things in this routine. Especially since you are using the ActiveX commands inside this routine. I've not yet learned enough about it to fully understand all those commands.

Share this post


Link to post
Share on other sites
ronjonp

If you are familiar with DXF codes then you need to check if 62 exists and 420 does not it's an index. This will convert the vla-object to an ename. Can you figure it out from here?

(setq e (vlax-vla-object->ename b))

 

Share this post


Link to post
Share on other sites
Shameless1

No. This is why I put it out here to the lisp guru's. I haven't tried writing lisp routines in like 15 years. Additionally, I was never familiar with ActiveX aspects. Back then I was more likely to piece/splice a couple routines together to get them to do what I wanted. So, that's where I am at in all this.

Share this post


Link to post
Share on other sites
ronjonp
12 hours ago, Shameless1 said:

No. This is why I put it out here to the lisp guru's. I haven't tried writing lisp routines in like 15 years. Additionally, I was never familiar with ActiveX aspects. Back then I was more likely to piece/splice a couple routines together to get them to do what I wanted. So, that's where I am at in all this.

I updated the code above to check for index colors. If the object is already color by layer, it uses the index color of the layer it resides on. 

Share this post


Link to post
Share on other sites
Shameless1

@ronjonp That's awesome. I knew what I was thinking and how I thought it should be implemented but I couldn't figure out how that would incorporate. I was also thinking it should slide in on the COND section and that's where you put it. I also appreciate the remarks for me to follow along, as well as the added line to create layer prefix based on object type instead of linework. I like the option. Thanks for all the great work on this.

Share this post


Link to post
Share on other sites
f700es

Is this lisp ready? I do Illustrator to CAD every know and then.

Share this post


Link to post
Share on other sites
Shameless1
8 hours ago, f700es said:

Is this lisp ready? I do Illustrator to CAD every know and then.

 

Yes.... see the above routine by @ronjonp. He revised the code in that initial response. However, I am not sure what your end goal is once in AutoCAD. I do find the splines are challenging to work with, manipulate, trim, extend, etc. I often convert to polylines with extremely high precision to maintain the integrity of the actual Illustrator lines. This creates additional issues with high vertices count polylines. Then I try everything I can to minimize the vertices count while still maintaining the highest degree of accuracy. I have used pldiet.lsp but it is not without its own challenges.

 

There is some quality software that seems to be the best in that area but you have to spend the money. CURVEFIT appears to be what I need, I'm just unwilling to spend the money at the moment. So, I'm always open to other alternatives. What do you typically do with regard to this?

Share this post


Link to post
Share on other sites
ronjonp
21 hours ago, Shameless1 said:

@ronjonp That's awesome. I knew what I was thinking and how I thought it should be implemented but I couldn't figure out how that would incorporate. I was also thinking it should slide in on the COND section and that's where you put it. I also appreciate the remarks for me to follow along, as well as the added line to create layer prefix based on object type instead of linework. I like the option. Thanks for all the great work on this.

Glad to help out! 🍻

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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