Create CSV files from attributes

Create CSV files from attributes

msmith_ncl
Enthusiast Enthusiast
955 Views
6 Replies
Message 1 of 7

Create CSV files from attributes

msmith_ncl
Enthusiast
Enthusiast

Hope someone can help with the below.  Trying to export attributes to multiple csv files from dynamic blocks,  I have some blocks that are not being selected and others that are being ignored if they don't have all attributes visible that are in the foreach lines.   I need all blocks included in the csv files, with blank cells if that attribute is currently not visible, block to be ignored if none of the attributes in the foreach lines are visible.  I have attached a DWG with some dynamic blocks inserted, these block names are not final, there will be more added with different block names.   Code below, any help is appreciated.

(Defun c:TripleCSV ( / _comma _Visible data ss i ev dp attb a b c folder opf csvfile)
(setq _comma (lambda (lst)(substr
(apply 'strcat
(mapcar '(lambda (str)
(strcat "," str) ) lst)
) 2
)
)
)
(defun _Visible (l) (Vl-remove-if '(lambda (atv)
(zerop (car atv))) l))
(and
(setq ReferenceNumber (strcase (getstring "\nEnter Reference Number: ")))
(snvalid ReferenceNumber )
(setq data nil
ss (ssget '((0 . "INSERT")(66 . 1))))
(repeat (setq i (sslength ss))
(setq ev (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
(if (and
(setq vp (Vl-some '(lambda (dy)
(if (eq (Vlax-get dy 'PropertyName) "Lookup1")
(Vlax-get dy 'Value)))
(Vlax-invoke ev 'GetDynamicBlockProperties)))
(setq attb (mapcar '(lambda (atb)
(list (vlax-get atb 'Visible )(vla-get-tagstring atb)
(Vla-get-textstring atb)))
(Vlax-invoke ev 'GetAttributes)
)
)
)
(setq data (cons (mapcar 'cdr (_Visible attb)) data ))
)
)
(setq folder (acet-ui-pickdir (Getvar 'dwgprefix)))
(setq data (vl-sort data
'(lambda (n m)
(< (cadr (assoc "ID_TAG" n))
(cadr (assoc "ID_TAG" m))
)
)
)
)

(foreach itm '(("_Devices"
("DEVICE_NUMBER" "DEVICE_TYPE" "PANEL_NUMBER" "PORT_NUMBER" "INPUT_NUMBER" "INPUT_TYPE" "OUTPUT_NUMBER" "OUTPUT_TYPE" ))
("_Inputs"
("DEVICE_NUMBER" "PANEL_NUMBER" "PORT_NUMBER" "INPUT_NUMBER" "INPUT_TYPE" ))
("_Outputs"
("DEVICE_NUMBER" "PANEL_NUMBER" "PORT_NUMBER" "OUTPUT_NUMBER" "OUTPUT_TYPE" ))
("_Panels"
("PANEL_TYPE" "PANEL_NUMBER" ))
)

(setq opf (open (setq csvfile (strcat folder "\\" ReferenceNumber (Car itm) ".csv")) "w"))
(write-line (_comma (setq taglst (Cadr itm))) opf)
(Foreach val data
(if (vl-every '(lambda (j)(assoc j val)) (Cadr itm))
(write-line
(_comma (mapcar '(lambda (v)
(cadr (assoc v val))) taglst)) opf)
)
)

(close opf)
(startapp "explorer" csvfile)
)
)
(princ)
)

 

0 Likes
Accepted solutions (1)
956 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

global attribute extractor v3.1  from lee mac, you can get the job done

0 Likes
Message 3 of 7

msmith_ncl
Enthusiast
Enthusiast

Thanks, I have this already, it works great.  I just want something that will create a standard set of CSV each time from 1 command, without any dialog box or extra input each time.

0 Likes
Message 4 of 7

msmith_ncl
Enthusiast
Enthusiast

Still hoping someone can help with this please?

0 Likes
Message 5 of 7

pbejse
Mentor
Mentor

@msmith_ncl wrote:

I have some blocks that are not being selected and others that are being ignored if they don't have all attributes visible that are in the foreach lines. 


Regardless if you add this line on the code. [ with reference to your sample posted somewhere else but NOT here]

 

("_Panels"
("PANEL_TYPE" "PANEL_NUMBER" ))
)

It will still be ignored, the block needs to have the "Lookup1" DB property for it to be included

its one of the requirement of the code.

 

(if (eq (Vlax-get dy 'PropertyName) "Lookup1")
			(Vlax-get dy 'Value)
		      )

Tell me if the blocks will eventually be having a LookUp DB property or will remain as regular block so we can work around it.

 


@msmith_ncl wrote:

I need all blocks included in the csv files, with blank cells if that attribute is currently not visible, block to be ignored if none of the attributes in the foreach lines are visible. 


The code can be easily adjusted to do just that, but first we will deal with the issue above

 


@msmith_ncl wrote:

 I have attached a DWG with some dynamic blocks inserted, these block names are not final, there will be more added with different block names.  

 


Exactly why requesters need to explain eveything from the get-go . In this particualr case, adding items to the existing list wont automatically make it work.

 

Main thing here is we deal with the first issue of including block names and the rest will follow

 

 

0 Likes
Message 6 of 7

msmith_ncl
Enthusiast
Enthusiast

I think I understand the questions, I don't have any lookups called "lookup1" in the final blocks so this is probably why this isn't working, now using a block table for visibility changes.

There are many other parameters in the blocks for moving things in different visibility states, can these be used?

 

0 Likes
Message 7 of 7

pbejse
Mentor
Mentor
Accepted solution

@msmith_ncl wrote:

I think I understand the questions, I don't have any lookups called "lookup1" in the final blocks so this is probably why this isn't working, now using a block table for visibility changes.

There are many other parameters in the blocks for moving things in different visibility states, can these be used?

 


We will stick with block and tag names, no need to worry about the DP's, there is the matter of adding new entries.

I'm not in favour of hardcoding data that you know will include more in the future, unless you are the only one using it then it's fine, but there's always the problem of  inadvertently messing up the code. Better to use an external file that you can ammend anytime.

 

 

 

 

 

 

0 Likes