Jump to content

Recommended Posts

Posted

Hi all, 

I am trying to export attributes to existing excel with below script. I don't know the reason it writes only the "PART_1" attribute value but not the sum of quantity attribute values. Can anyone help me to get the values into excel.

I believe have some problem with below part in my script. I am a basic scripter not sure how to fix the issue.

 

(if (and a b)
(progn
(if (assoc a lst)
(setq lst (subst (cons a (+ b (cdr (assoc a lst)))) (assoc a lst) lst))
(setq lst (cons (cons a b) lst))
);; if
(setq a nil b nil)
);; progn
);; if


When I use the above code in my script I am getting the values in the below format


(("PART01" . 6.0)("PART02" . 4.0)("PART03" . 3.5))


I believe if the list comes in below format the values might extract to excel.


(("PART01" . "6.0")("PART02" . "4.0")("PART03" . "3.5"))

 

 

*************************************

;;Main script  how I listed the entities;;

;; Block have multiple attributes but only two attributes required to extract;;;

 

(setq s1 (ssget "_x"(list '(0 . "INSERT")'(2 . "Machine,`**")'(66 . 1)(cons 410 (getvar 'CTAB)))))


(repeat
(setq i (sslength s1))
(setq hnd (ssname s1 (setq i (1- i))))
(setq ent (entget hnd))
(setq attlst nil)
(setq obj (vlax-ename->vla-object hnd))
(setq attlst (vlax-invoke obj 'GetAttributes));; setq
(foreach att attlst
(cond ((and (= (strcase (vla-get-TagString att)) "PART_1")
(/= (vla-get-TextString att) ""))
(setq a (vla-get-TextString att))
)
((and (= (strcase (vla-get-TagString att)) "Quantity")
(/= (vla-get-TextString att) ""))
(setq b (atof (vla-get-TextString att)))
)
);; cond
);; foreach

(if (and a b)
(progn
(if (assoc a lst)
(setq lst(subst (cons a (+ b (cdr (assoc a lst)))) (assoc a lst) lst))
(setq lst (cons (cons a b) lst))
);; if
(setq a nil b nil)
);; progn
);; if

);; repeat


(if lst
(progn
(setq lst (vl-sort lst (function (lambda (a b) (< (car a) (car b))))))
(setq i 0)


(setq tmp_lst (reverse lst))

(setq sel_lst (cons '("PART NAME." "Quantity") sel_lst))
(setq sel_lst (append sel_lst lst))

 

(gc:WriteExcel file nil nil sel_lst)

 

*********************************************************************

Posted

I know some people like dotted pairs in this case not sure why not just make a list of 2 items can set a nil to say "-"

 

(("PART01"  6.0)("PART02"   4.0)("PART03"   3.5))

or 

(("PART01"  "6.0")("PART02"  "4.0")("PART03"  "3.5"))

 

so (nth x masterlist = ("PART01"  "6.0")

so (nth 0 (nth x masterlist ="PART01"

so (nth 1 (nth x masterlist = 6.0

 

(setq lst (cons (list a b) lst))

(("part01" 6.0))

 

(setq lst (cons (list a (rtos b 2 2)) lst))

(("part01" "6")) ; note integer as no decimal value.

 

Posted

Thank you for your reply Bigal. Still no luck . Coming an error like in the below format

 

error : bad argument type <("6")> ; expected <NUMBER> at [+]

 

I have only updated below part in my script. is it correct?

(setq lst (cons (list a (rtos b 2 2)) lst))

Posted

Hi all , any update on my query. I am still searching in internet to find a solution for my query but I couldn't. Please someone can help me to resolve my issue.

Posted (edited)

haven't looked at the complete code but this isn't gonna work : (= (strcase (vla-get-TagString att)) "Quantity")

 

try this

(defun c:savinirsb4u  (/ s1 i hnd ent attlst att a b lst temp_lst sel_lst file)
  (setq file "c:\\Temp\\savinirsb4u.xlsx"); put your own filename here
  (setq s1 (ssget "_x" (list '(0 . "INSERT") '(2 . "Machine,`**") '(66 . 1) (cons 410 (getvar 'CTAB)))))
  (repeat
    (setq i (sslength s1))
    (setq hnd (ssname s1 (setq i (1- i))))
    (setq ent (entget hnd))
    (setq attlst nil)
    (setq obj (vlax-ename->vla-object hnd))
    (setq attlst (vlax-invoke obj 'GetAttributes))
    (foreach att attlst
      (cond
        ((and (= (strcase (vla-get-TagString att)) "PART_1")  (/= (vla-get-TextString att) ""))(setq a (vla-get-TextString att)))
        ((and (= (strcase (vla-get-TagString att)) "QUANTITY")(/= (vla-get-TextString att) ""))(setq b (atof (vla-get-TextString att))))
      )
    )
    (if (and a b)
      (progn
        (if (assoc a lst)
          (setq lst (subst (cons a (+ b (cdr (assoc a lst)))) (assoc a lst) lst))
          (setq lst (cons (cons a b) lst))
        )
        (setq a nil b nil)
      )
    )
  )
  (setq lst (mapcar '(lambda (x)(list (car x) (cdr x))) lst))
  (if lst
    (progn
      (setq lst (vl-sort lst (function (lambda (a b) (< (car a) (car b))))))
      (setq i 0)
      (setq tmp_lst (reverse lst))
      (setq sel_lst (cons '("PART NAME." "Quantity") sel_lst))
      (setq sel_lst (append sel_lst lst))
    )
  )
  (princ)
  (gc:WriteExcel file nil nil sel_lst)
)
Edited by rlx
  • Like 1
Posted

Wow. Thank you RLX.. above code is perfectly working what I want . Thank you for your time.

 

Thank you all who tried  to help me with my code.

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