Jump to content

output window selection set to CSV


mdwin

Recommended Posts

I have a LISP that i borrowed from Commandobill and Lee Mac. I am trying to modify it to allow a windowed selection set

example: (setq Text_SS (ssget "_W" (list 16.8 10.3)(list 18.9 5.0) '((0 . "TEXT"))))

 

and output all in one string separated by comma's. This currently ouputs each text into a separate row. Many thanks to Commandobill and Lee Mac, I have already learned a lot from your previous posts.

 

Any idea's would be appreciative.

 

code

(defun c:txt2xl (/ fPath fName ofile doc ss)
 (vl-load-com)

 (setq fPath (getvar "dwgprefix"))
 ;; Obtain the file path of the drawing
 (setq fName (substr (getvar "dwgname")
       1
       (- (strlen (getvar "dwgname")) 4)
      )
 )
 ;; remove the .dwg from the file name
 (if (vl-file-directory-p fPath)
   (progn
     (setq ofile (open (strcat fPath fName " DWG.csv") "a"))
     (vlax-for doc (vla-get-Documents
       (vlax-get-Acad-Object)
     )
(if (setq ss (ssget "_X" (list (cons 0 "TEXT") (cons 8 "text"))))
  (mapcar
    (function
      (lambda (x)
 (write-line x ofile)
      )
    )
    (mapcar
      (function
 (lambda (x)
   (cdr (assoc 1 (entget x)))
 )
      )
      (mapcar 'cadr (ssnamex ss))
    )
  )
)
(setq ss nil)
     )
     (close ofile)
   )
   (princ
     "\n<!> Filename Does not Refer to Valid Directory <!>"
   )
 )
 (princ)
)

Link to comment
Share on other sites

I think this may be a better approach :)

 

(defun c:txt2xl  (/ fPath fName ofile doc oStr)
 (vl-load-com)

 (setq fPath (getvar "dwgprefix"))
 ;; Obtain the file path of the drawing
 (setq fName (substr (getvar "dwgname") 1
                     (- (strlen (getvar "dwgname")) 4)))
 ;; remove the .dwg from the file name
 (if (and (vl-file-directory-p fPath)
          (setq pt1 (getpoint "\nSelect First Point of Window: "))
          (setq pt2 (getcorner pt1 "\nSelect Second Point of Window: ")))
   (progn
     (setq ofile (open (strcat fPath fName " DWG.csv") "a"))
     (vlax-for doc  (vla-get-Documents
                      (vlax-get-Acad-Object))
       (setq oStr "")
       (vlax-for lay (vla-get-layouts doc)
         (vlax-for Obj (vla-get-Block lay)
           (if (eq "AcDbText" (vla-get-ObjectName Obj))
             (setq oStr (strcat oStr (vla-get-TextString Obj) (chr 44))))))
       (write-line (substr OStr 1 (1- (strlen oStr))) ofile))
     (close ofile))
   (princ "\n<< Incorrect Selection or Bad Path >>"))
 (princ))

Link to comment
Share on other sites

Thanks for your help Lee Mac, this is just the layout im looking for. one big row separated by comma's.

It seems when i run the code it ignores any selection set and just looks for all text on the drawing.

 

Im looking to create a window and in that window pull out text from the layer "TEXT". It is the same window size for each drawing, im creating a batch program to upload the info into another database, but its proving quite cumbersome LOL.

 

if you have any other idea's i would greatly appreciate it. Thanks again.

Link to comment
Share on other sites

Maybe something a little simpler:

 

[b][color=BLACK]([/color][/b]defun c:txt2xl [b][color=FUCHSIA]([/color][/b]/ text_ss i wf en tv[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq text_ss [b][color=MAROON]([/color][/b]ssget [color=#2f4f4f]"_W"[/color] '[b][color=GREEN]([/color][/b]16.8 10.3[b][color=GREEN])[/color][/b]
                                '[b][color=GREEN]([/color][/b]18.9  5.0[b][color=GREEN])[/color][/b] '[b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]0 . [color=#2f4f4f]"TEXT"[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]setq i [b][color=MAROON]([/color][/b]sslength text_ss[b][color=MAROON])[/color][/b]
           wf [b][color=MAROON]([/color][/b]open [color=#2f4f4f]"dwg.csv"[/color] [color=#2f4f4f]"w"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]minusp [b][color=BLUE]([/color][/b]setq i [b][color=RED]([/color][/b]1- i[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname text_ss i[b][color=GREEN])[/color][/b]
                   tv [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 1 [b][color=RED]([/color][/b]entget en[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]princ tv wf[b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]princ [color=#2f4f4f]","[/color] wf[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]close wf[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

-David

Link to comment
Share on other sites

very nice. Thanks david, it is perfect. I appreciate the help from both of you, i spent weeks attempting to learn the lisp language but i couldnt get it to do just what i wanted it to. Thanks again David and Lee Mac.

Link to comment
Share on other sites

Thanks for your help Lee Mac, this is just the layout im looking for. one big row separated by comma's.

It seems when i run the code it ignores any selection set and just looks for all text on the drawing.

 

Im looking to create a window and in that window pull out text from the layer "TEXT". It is the same window size for each drawing, im creating a batch program to upload the info into another database, but its proving quite cumbersome LOL.

 

if you have any other idea's i would greatly appreciate it. Thanks again.

 

Sorry, I thought you wanted it done to every open document, so I spent more time on that and forgot the part about the window selection... :oops:

Link to comment
Share on other sites

no need to apologize. im running it with ezscript to automate the extraction process.

 

I ran a couple tests and noticed that it is pulling the info at random even though its in the same spot on each drawing.

 

Is there any way to put some kind of sort by "X or Y" so that it will be more consistant in its extraction?

 

Im going to try a few things and see what i come up with, but if you get another spare minute i would appreciate it. Thanks.

Link to comment
Share on other sites

Hopefully this should help with the sorting - should sort by x coord.

 

Sorry David, used some of your code:

 

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] c:txt2xl [b][color=RED]([/color][/b][b][color=BLUE]/[/color][/b] text_ss wf[b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]vl-load-com[/color][/b][b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] text_ss [b][color=RED]([/color][/b][b][color=BLUE]ssget[/color][/b] [b][color=#ff00ff]"_W"[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=RED]([/color][/b][b][color=#009999]16.8[/color][/b] [b][color=#009999]10.3[/color][/b][b][color=RED])[/color][/b]
                                [b][color=DARKRED]'[/color][/b][b][color=RED]([/color][/b][b][color=#009999]18.9[/color][/b]  [b][color=#009999]5.0[/color][/b][b][color=RED])[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=RED]([/color][/b][b][color=RED]([/color][/b][b][color=#009900]0[/color][/b] . [b][color=#ff00ff]"TEXT"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
      [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] wf [b][color=RED]([/color][/b][b][color=BLUE]open[/color][/b] [b][color=#ff00ff]"dwg.csv"[/color][/b] [b][color=#ff00ff]"w"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
      [b][color=RED]([/color][/b][b][color=BLUE]foreach[/color][/b] txt [b][color=RED]([/color][/b][b][color=BLUE]vl-sort[/color][/b]
                     [b][color=RED]([/color][/b][b][color=BLUE]vl-remove-if[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]listp[/color][/b]
                       [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]cadr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]ssnamex[/color][/b] text_ss[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
                         [b][color=RED]([/color][/b][b][color=BLUE]function[/color][/b]
                           [b][color=RED]([/color][/b][b][color=BLUE]lambda[/color][/b] [b][color=RED]([/color][/b]a b[b][color=RED])[/color][/b]
                             [b][color=RED]([/color][/b][b][color=BLUE]<[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cadr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]10[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] a[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
                                [b][color=RED]([/color][/b][b][color=BLUE]cadr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]10[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] b[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
        [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]1[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] txt[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] wf[b][color=RED])[/color][/b]
        [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=#ff00ff]","[/color][/b] wf[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
      [b][color=RED]([/color][/b][b][color=BLUE]close[/color][/b] wf[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]prin1[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]

Link to comment
Share on other sites

sweet thanks. ive been playing around with the assoc #'s in the formula and they seem to switch around the sorting, but help doesnt really specify anything.

 

could you tell me what i would need to change to make it sort by Y or is that a completely different formula? Thanks.

Link to comment
Share on other sites

The sorting is done by this part of the code:

 

(vl-sort
 (vl-remove-if 'listp
   (mapcar 'cadr (ssnamex text_ss)))
     (function
       (lambda (a b)
         (< (cadr (assoc 10 (entget a)))
            (cadr (assoc 10 (entget b)))))))

 

vl-sort sorts a list using a comparison function:

 

(vl-sort [color=Red][i]<list>[/i][/color] [i][color=Red]<comparison function>[/color][/i])

 

In this case the list is:

 

 (vl-remove-if 'listp
   (mapcar 'cadr (ssnamex text_ss)))

 

Which is just a list of entity names in the selection set.

 

The comparison function is:

 

     (function
       (lambda (a b)
         (< (cadr (assoc 10 (entget a)))
            (cadr (assoc 10 (entget b))))))

 

Where lambda is just an anonymous function name - an alternative to defun... if you will.

 

(cdr (assoc 10 (entget a)))  --  Get Insertion Point

(cadr (assoc 10 (entget a))) -- Get X-coord of insertion point

[i][  (caddr (assoc 10 (entget a))) -- Get Y-coord of insertion point  ][/i]

 

As the comparison goes:

 

-- Sort X coord, lowest first --
     (function
       (lambda (a b)
         (< (cadr (assoc 10 (entget a)))
            (cadr (assoc 10 (entget b))))))

-- Sort X coord, highest first --

     (function
       (lambda (a b)
         (> (cadr (assoc 10 (entget a)))
            (cadr (assoc 10 (entget b))))))

 

Hope this helps :)

 

Lee

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