XREF Draw Order getting the list from CSV

XREF Draw Order getting the list from CSV

nichkherbsman
Advocate Advocate
1,252 Views
11 Replies
Message 1 of 12

XREF Draw Order getting the list from CSV

nichkherbsman
Advocate
Advocate

Hi,

I need help. I use this code but I get an error.

I want to draworder the xref file base on the list in csv.

 

Thank you.

 

;;CODE

(defun get_order (/ rs)
(setq rs '(getfiled "Select a .csv file to read" "C:/" "csv" 8))
)
;; SORT XREFS
(defun xreforder (/ xo ss)
(setvar 'cmdecho 0)
(setvar 'tilemode 1)
(setq xo (get_order))
(foreach obj xo
(if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 obj))))
(command "_.draworder" ss "" "_b")
)
)
(setvar 'cmdecho 1)
(setvar 'tilemode 0)
(princ)
)(princ)

(defun c:xrotest nil (xreforder))

NiCHKCiD
BIM Modeler
Technical Officer
Technical Designer
Sr. Draftsman
0 Likes
Accepted solutions (2)
1,253 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

It always helps to convey what the error message is.  But in any case....

 

Your xo variable is going to contain a filepath and filename, but the (foreach) function is going to require a list.  There needs to be an (open) function to get into that file, and at least one (read-line) function to pull information from it, and some kind of conversion into a list, before you can step through anything.  How you get the Xref names into a list to step through depends on the format of the information in the .csv file -- is it one line with comma delimiters, or is each name on its own line, or some other arrangement?

 

Also, there are two issues with this line:

(setq rs '(getfiled "Select a .csv file to read" "C:/" "csv" 8))

 

First:  the rs variable is never used, so you can dump it and its (setq) wrapper function, and just have the (get_order) sub-routine feed the result directly into the xo variable.  Or, since that sub-routine is used only once, you could skip defining that as a function entirely, and just go straight for what you need where you need it:

 

(setq xo (getfiled "Select a .csv file to read" "C:/" "csv" 8))

 

Second:  the apostrophe before '(getfiled should not be there.

 

[And a little pet peeve of mine....  It always looks silly to see the first Message in a thread, which is raising the question or posing the problem, asking someone (who for the most part can't be someone else, only you) to Accept it as a Solution.  Message 1 can never the the Solution.  I suggest you remove that from your Signature, and make yourself a Macro for it, that you can plug into a Message when actually offering something that might be a Solution.]

Kent Cooper, AIA
0 Likes
Message 3 of 12

JamesMaeding
Advisor
Advisor
Accepted solution

@nichkherbsman 

please indent your lisp code based on nesting level.

Use this function to convert a text file to list of lines:

(defun FILE-TO-LST-ALL (Fil
                        / FilObj FilPth FilSys OpnFil RetVal
                        FileH TextString)
 (if (setq FilPth (findfile Fil))
  (progn
   (setq FilSys (vlax-create-object "Scripting.FileSystemObject")
         FilObj (vlax-invoke FilSys "GetFile" FilPth)
         OpnFil (vlax-invoke FilObj "OpenAsTextStream" 1 0)
   )
   (while (= (vlax-get OpnFil "AtEndOfStream") 0)
    (setq RetVal (cons (vlax-invoke OpnFil "ReadLine") RetVal))
   )
   (vlax-invoke OpnFil "Close")
   (vlax-release-object OpnFil)
   (vlax-release-object FilObj)
   (vlax-release-object FilSys)
   (setq RetVal (reverse RetVal))
   (if (and (> (length RetVal) 0) 
            (> (strlen (car RetVal)) 2)
            (= (substr (car RetVal) 1 3) "")
       )
     (setq RetVal (cons (substr (car RetVal) 4)(cdr RetVal)))
   )
   RetVal
  )
  nil
 )
)

so change your function to:

(defun get_order (/ rs)
  (setq rs (getfiled "Select a .csv file to read" "C:/" "csv" 8))
  (FILE-TO-LST-ALL rs)
)

internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

Message 4 of 12

JamesMaeding
Advisor
Advisor

btw, Ive never seen this:

(defun c:xrotest nil (xreforder))

use this:

(defun c:xrotest () (xreforder))

for command creation.


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

0 Likes
Message 5 of 12

nichkherbsman
Advocate
Advocate

Thank you for the help and suggestion. I am not that good in lisp that's why I really need help to correct this code.
I will try to do your suggestion and have an experiment of it.
About the signature, I don't really put signature on my post.

NiCHKCiD
BIM Modeler
Technical Officer
Technical Designer
Sr. Draftsman
0 Likes
Message 6 of 12

nichkherbsman
Advocate
Advocate

Thank you very much. I will try this code.

NiCHKCiD
BIM Modeler
Technical Officer
Technical Designer
Sr. Draftsman
0 Likes
Message 7 of 12

nichkherbsman
Advocate
Advocate

(defun get_order (/ rs)
(setq rs (getfiled "Select a .csv file to read" "C:/" "csv" 8))
(FILE-TO-LST-ALL rs)
)
;; SORT XREFS
(defun FILE-TO-LST-ALL (Fil
/ FilObj FilPth FilSys OpnFil RetVal
FileH TextString)
(if (setq FilPth (findfile Fil))
(progn
(setq FilSys (vlax-create-object "Scripting.FileSystemObject")
FilObj (vlax-invoke FilSys "GetFile" FilPth)
OpnFil (vlax-invoke FilObj "OpenAsTextStream" 1 0)
)
(while (= (vlax-get OpnFil "AtEndOfStream") 0)
(setq RetVal (cons (vlax-invoke OpnFil "ReadLine") RetVal))
)
(vlax-invoke OpnFil "Close")
(vlax-release-object OpnFil)
(vlax-release-object FilObj)
(vlax-release-object FilSys)
(setq RetVal (reverse RetVal))
(if (and (> (length RetVal) 0)
(> (strlen (car RetVal)) 2)
(= (substr (car RetVal) 1 3) "")
)
(setq RetVal (cons (substr (car RetVal) 4)(cdr RetVal)))
)
RetVal
)
nil
)
)

(defun xreforder (/ xo ss)
(setvar 'cmdecho 0)
(setvar 'tilemode 1)
(setq xo (get_order))
(foreach obj xo
(if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 obj))))
(command "_.draworder" ss "" "_b")
)
)
(setvar 'cmdecho 1)
(setvar 'tilemode 0)
(princ)
)(princ)

(defun c:xrotest () (xreforder))

NiCHKCiD
BIM Modeler
Technical Officer
Technical Designer
Sr. Draftsman
0 Likes
Message 8 of 12

nichkherbsman
Advocate
Advocate

Additional File

NiCHKCiD
BIM Modeler
Technical Officer
Technical Designer
Sr. Draftsman
0 Likes
Message 9 of 12

nichkherbsman
Advocate
Advocate

Is the combination of code are correct?

 

Thank you

NiCHKCiD
BIM Modeler
Technical Officer
Technical Designer
Sr. Draftsman
0 Likes
Message 10 of 12

cadffm
Consultant
Consultant

@JamesMaeding  schrieb:

btw, Ive never seen this:

(defun c:xrotest nil (xreforder))

use this:

(defun c:xrotest () (xreforder))

for command creation.


Offtopic

Just wanted to mention: No matter   () or nil, nil is nil  

  () is just the same: NotInList, short: NIL

not a problem

 

Sebastian

0 Likes
Message 11 of 12

JamesMaeding
Advisor
Advisor
Accepted solution

@cadffm 

Now that is some cool trivia - NIL = Not In List!

I thought it was some British term for nothing.

@nichkherbsman 

You need to learn and use the VLIDE that is part of acad. Just type VLIDE to open.

Got to tools->window attributes->configure current and set tab width to 2:

tab.jpg

Say yes to use as prototype....

The VLIDE color codes things to help you, and you can double click before or after a paren to select that block of code.

That allows you to match parens easily, and catch mistakes.

Also, you can select a statement, right click, and inspect to run it and see the result.

So to test my FILE-TO-LST-ALL function, type with some real text file name:

(FILE-TO-LST-ALL "C:\\temp\\CALC ARC TABLE.dat")

and you get a window showing the result.

You can double click on a list and it expands it.

Also look up setting a break point so you can stop the code mid-run, and look at vars using inspect.

You have likely already wasted more time by not knowing the VLIDE than it takes to try this so have at it.

Note that Bricscad has the same thing, though the debugging is different in a few ways, better and worse.


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

0 Likes
Message 12 of 12

nichkherbsman
Advocate
Advocate

Thank you so much for the information.

NiCHKCiD
BIM Modeler
Technical Officer
Technical Designer
Sr. Draftsman
0 Likes