Author Topic: help combining 3 codes into one  (Read 1993 times)

0 Members and 1 Guest are viewing this topic.

Ricky S

  • Newt
  • Posts: 24
help combining 3 codes into one
« on: April 17, 2020, 12:27:26 PM »
I need a lisp program that will allow the user to select items on screen, place them on a new layer named cut, explode all polylines, and create a dxf file out of all the pieces.  I have been able to break the task into 3 parts and kluge together some really crude code that is working for me, but every time I try to combine the codes I fail miserably! I know there has to be a much more elegant way to code this but the truth is I'm in over my head an need some Help!

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help combining 3 codes into one
« Reply #1 on: April 17, 2020, 01:08:59 PM »
1. Should selection be limited to plines?
2. All plines or only 2D plines?
3. Should export be limited to lines born of exploded plines?
4. Can you provide a representative sample dwg?
5. tl/dr: Need more deets.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help combining 3 codes into one
« Reply #2 on: April 17, 2020, 01:28:10 PM »
3B. Should export include / exclude arcs born of exploded plines?
3C. Should z values be included, excluded, normalized?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Ricky S

  • Newt
  • Posts: 24
Re: help combining 3 codes into one
« Reply #3 on: April 17, 2020, 02:06:08 PM »
The Output can be lines, circles, arcs,  and exploded polylines.  The DXF file will be handed off to a plasma table for cutting, it cannot handle polylines and everything it cuts has to be on a layer named "cut".
1. limited to poly lines - No
2. Only 2d
3. no
3b. inlude anything that is exploded from a polyline
3c. all our files are 2d only.
4. see sample base plate attached
5. let me know if you need more

Thanks for looking at it

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help combining 3 codes into one
« Reply #4 on: April 17, 2020, 02:22:25 PM »
Thanks for responding. A representative sample dwg, i.e. objects as existing before processing, would be helpful. Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Ricky S

  • Newt
  • Posts: 24
Re: help combining 3 codes into one
« Reply #5 on: April 17, 2020, 02:29:42 PM »
Here is the drawing the DXF file was produced from.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help combining 3 codes into one
« Reply #6 on: April 17, 2020, 05:25:18 PM »
Not the prettiest code but try it nonetheless. Does everything (I think) you asked in one pass. What you may notice is that even tho it will explode plines for the dxf export, the original plines remain in the host drawing - any objects spawned from explode call are considered temporary, and removed after the command completes.

Code: [Select]
(defun c:Whargarbl ( / ss sst fn doc layer layers i e x spawn temp fly-ball cmdecho )

    (vl-load-com)

    (cond
        (   (null (setq ss (ssget '((0 . "arc,circle,line,lwpolyline,polyline")))))
            (princ "\nNothing selected, command cancelled.")
        )
        (   (null (setq fn (getfiled "DXF File:" (getvar 'dwgprefix) "dxf" 1)))
            (princ "\nOutput file not specified, command cancelled.")
        )
        (   (progn
                (vl-catch-all-apply 'vl-file-delete (list fn))
                (findfile fn)
            )
            (princ (strcat "\nOutput file <" fn "> is read-only, command cancelled."))
        )
        (   (setq
                doc    (vla-get-activedocument (vlax-get-acad-object))
                layer  "Cut"
                layers (vla-get-layers doc)
                sst    (ssadd)
            )
            (vla-add layers layer)
            (vlax-for x layers (vla-put-lock x :vlax-false))
            (repeat (setq i (sslength ss))
                (cond
                    (   (and
                            (setq x (vlax-ename->vla-object (setq e (ssname ss (setq i (1- i))))))
                            (vlax-method-applicable-p x 'explode)
                            (progn
                                (setq spawn (vl-catch-all-apply 'vlax-invoke (list x 'explode)))
                                (< 0 (length spawn))
                            )
                        )
                        (foreach o spawn
                            (vla-put-layer o layer)
                            (ssadd (vlax-vla-object->ename o) sst)
                        )
                        (setq temp (append spawn temp))
                    )
                    (   t
                        (vla-put-layer x layer)
                        (ssadd e sst)
                    )
                )
            )
            (setq cmdecho (getvar 'cmdecho))
            (setvar 'cmdecho 0)
            (if (vl-catch-all-error-p (setq fly-ball (vl-catch-all-apply 'vl-cmdf (list "_.dxfout" fn "_Objects" sst "" "_Version" "2000" ""))))
                (princ (strcat "\nError: " (vl-catch-all-error-message fly-ball) "."))
            )
            (mapcar 'vla-delete temp)
            (setvar 'cmdecho cmdecho)
        )
    )

    (princ)

)

Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Ricky S

  • Newt
  • Posts: 24
Re: help combining 3 codes into one
« Reply #7 on: April 17, 2020, 09:33:38 PM »
Wow! Just wow!  It does everything I asked and then some! like leaving the original plines intact (nice touch) I even like  the variable names, Thank you so much MP! I have questions, but I want to study this before I start asking.  You said it's not the prettiest code, but I think it's pretty awesome, and never would I have ever arrived at this on my own, so thank you! Hopefully you'll be up for a few questions next week as it's obvious I have a lot to learn!
Ricky

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help combining 3 codes into one
« Reply #8 on: April 17, 2020, 10:23:41 PM »
My pleasure Ricky, thanks for the kind gratitude. With any luck I’ll be able to answer questions that you float. Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Ricky S

  • Newt
  • Posts: 24
Re: help combining 3 codes into one
« Reply #9 on: April 21, 2020, 02:17:02 PM »
MP,  I have never used visual lisp, but I've been doing some reading, so I'm a new born newbie!  First question is on line 24 of your code  (vla-add layers layer)  is this creating the layer "Cut" that you (setq layer "Cut") back on line 20? 

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help combining 3 codes into one
« Reply #10 on: April 21, 2020, 03:55:13 PM »
First question is on line 24 of your code  (vla-add layers layer)  is this creating the layer "Cut" that you (setq layer "Cut") back on line 20?

Yes sir.

Small tangent - many collections will throw an error if you attempt to add a key that already exists in the collection. The layer collection simply returns the item if it already exists, ergo the naked (vla-add layers layer). The reason I mention this is down the road you may see code like ... (vl-catch-all-apply 'vla-add (list collection key)) ... to trap errors for collections that will throw an error if attempting to add a key that already exists.

Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Ricky S

  • Newt
  • Posts: 24
Re: help combining 3 codes into one
« Reply #11 on: April 21, 2020, 04:25:40 PM »
OH....Thanks for the reply, I ran into an error trying to add a color to the layer, so I was backing back through the code trying to make sense of it. I grasp the concept but I'm struggling with syntax, especially which portion of code is doing what...I tried adding (vla-put-color Layer 2) after line 24 of your code.  Mind you I don't really need to change color as much as I want to understand how the code is working, so I've been tinkering with it.  Any ways it threw a command error use command-s and never ran the code.  I ran into this on my code snippets and realized most current code does not even use the "command" call.....much much more to learn!  On a plus note I should have a few DXF files running through the plasma table next week, if all goes well I'll post a couple of pictures of the completed parts.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: help combining 3 codes into one
« Reply #12 on: April 21, 2020, 05:05:51 PM »
(posting from ipad)

;; revise line 24 to set the layer’s color to green ...
(vla-put-color (vla-add layers layer) 3)

This should work (untested) as the vla-add method returns the added object, thus it’s methods or properties can be invoked or accessed respectively as demonstrated.

Cheers,
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst