• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

vlisp: import geometry n excel elements for processing

  • Thread starter Thread starter Dr.Lube
  • Start date Start date

Dr.Lube

Guest
Hello everyone,
I try to be more exhaustive since in the title I can only use tot letters (and I used them all!) :)

for work I need to automate the calculation of the center of stiffness.
what I do is convert to polyline regions and go to extract the coordinates of the center and the main moments of inertia. everything is put on a support excel file where I will perform the necessary calculations to find the coordinates of the inertia center that I will then insert, as a point, inside autocad.

to make this use two lisp, that of data extraction is made by me and while that of export and forwarding data from excel I found it on the net.
the problem I have encountered is the inability to make a cycle with progressive index so as to move inside the excel file. in the import in fact I always overwrite the same line! another problem is that the values are imported to me with the point, instead of with the comma.
Code:
(vl-load-com)
(defun c:inerzia ( / acapp ent i xy ix iy x y)
(setq acapp (vlax-get-acad-object))
(set indexq 1)
(sing)
(Sectq ent (car(entsel)));
(setq ent (vlax-ename->vla-object ent));

(setq xy (vla-get-centroid ent)
(Sectq i (vla-get-principalmoments ent));

(setq ix (vlax-safearray-get-element (vlax-variant-value i) 0) ; variant is an array of 2 elements, extract the first ix
(setq iy (vlax-safearray-get-element (vlax-variant-value i)); see above, I extract the second iy
(setq x (vlax-safearray-get-element (vlax-variant-value xy) 0) ;coordinated center x
(setq y (vlax-safearray-get-element (vlax-variant-value xy) 1) ;coordinated y

;the idea now was to create an array that contains all the data of the while cycle. here is definitely wrong, it is saved at the last attempt I made to understand how it worked
(setq cord (vlax-make-safearray vlax-vbdouble '(1 . 3)))
(vlax-safearray-put-element cord 0 index ix)

; below is how I did it at first, without extracting data individually. . I had 2 lists that if inserted in exel were important "correct" in 4 columns
(setq i (vlax-safearray->list (vlax-variant-value i)))
(setq xy (vlax-safearray->list (vlax-variant-value xy))
; export of lists
(setq xl (vlax-get-or-create-object "excel.application"))
(openexcel "c:\\prova.xls" nil)
;apro the test.xls file located in that path, on the page "inertia" and with nil I do not see it appear in the taskbar
(putcell "a1" xy) ; insert in the cell
(putcell c1 i)
(closeexcel "c:\\prova.xls")

(setq index (1+ index)); increase the row counter
);

(princ)
); end
missing the part where I take the processed data, but I stepped by step:)
the question is therefore: how do I move forward?

below the code for the use of excel (Annex)

Thanks :)
dennis
 

Attachments

so it should work for writing the data on excel, di volta in volta, each selection of a region.
tell me if you want to write the data only onceAt the end of the selections.
Code:
(vl-load-com)
(defun c:inerzia ( / acapp ent i xy iy x y)
(setq acapp (vlax-get-acad-object))[COLOR="#FFA500"]    ;(setq index 1)[/COLOR][B][COLOR="#FF0000"]    (setq index 0)[/COLOR][/B](sing)
(Sectq ent (car(entsel)));
(setq ent (vlax-ename->vla-object ent));

(setq xy (vla-get-centroid ent)
(Sectq i (vla-get-principalmoments ent));

(setq ix (vlax-safearray-get-element (vlax-variant-value i) 0) ; variant is an array of 2 elements, extract the first ix
(setq iy (vlax-safearray-get-element (vlax-variant-value i)); see above, I extract the second iy
(setq x (vlax-safearray-get-element (vlax-variant-value xy) 0) ;coordinated center x
(setq y (vlax-safearray-get-element (vlax-variant-value xy) 1) ;coordinated y

;the idea now was to create an array that contains all the data of the while cycle. here is definitely wrong, it is saved at the last attempt I made to understand how it worked[COLOR="#FFA500"] (setq cord (vlax-make-safearray vlax-vbdouble '(1 . 3)))
(vlax-safearray-put-element cord 0 index ix)[/COLOR]; below is how I did it at first, without extracting data individually. . I had 2 lists that if inserted in exel were important "correct" in 4 columns
(setq i (vlax-safearray->list (vlax-variant-value i)))
(setq xy (vlax-safearray->list (vlax-variant-value xy))
; export of lists
(setq xl (vlax-get-or-create-object "excel.application"))
(openexcel "c:\\prova.xls" nil)
;apro the test.xls file located in that path, on the page "inertia" and with nil I do not see it appear in the taskbar[COLOR="#FFA500"] ;(putcell "a1" xy) ;I insert in the cell[/COLOR][B][COLOR="#FF0000"] 	(putcell (strcat "a" (itoa (setq index (1+ index)))) xy)[/COLOR][/B][COLOR="#FFA500"] ;(putcell "c1" i)[/COLOR][B][COLOR="#FF0000"] (putcell (strcat "c" (itoa index) i)[/COLOR][/B](closeexcel "c:\\prova.xls")
 [COLOR="#FFA500"] ;(setq index (1+ index)); increase the row counter[/COLOR]
    );while

    (princ)
);fine
 
Wow! Congratulations, I had taken 2 days to do that part:) thank you for making me notice the mistakes!!

Yes, indeed the ideal would be to be able to select everything in a row and open the file to write to us only at the end (so as to speed up the code).
How can you solve the problem of the point (replacement with comma) ? is there also a way not to lean on the excel file and make producers and divisions?

Thank you very much :))
 
...how can you solve the problem of the point (replacement with comma)?...
so:
Code:
(vl-load-com)
(defun c:inerzia ( / acapp ent i xy iy x y)
(setq acapp (vlax-get-acad-object))
(setq index 0)
(while
(setq ent (car(entsel))) ;selezioni l'entità
(setq ent (vlax-ename->vla-object ent) ;lo strasformo in an oggetto

(setq xy (vla-get-centroid ent))
(setq and (vla-get-principalmoments ent) ; straggo and dati

(setq ix (vlax-safearray-get-element (vlax-variant-value i) 0) ;il variant is a 2 element array, estraggo il primo ix
(setq iy (vlax-safearray-get-element (vlax-variant-value i) 1) ;vedi sopra, estraggo il secondo iy
(setq x (vlax-safearray-get-element (vlax-variant-value xy) 0) ;coordinate centro x
(setq y (vlax-safearray-get-element (vlax-variant-value xy) 1) ;coordinate centro y

(setq i (vlax-safearray->list (vlax-variant-value i)))
(setq xy (vlax-safearray->list (vlax-variant-value xy)))

Esportazione delle liste
(setq xl (vlax-get-or-create-object "excel.application"))

;apro il file prova.xls che si trova in quel path, alla pagina "inerzia" e con nil non lo vedo comparire nella taskbar
(openexcel "c:\\prova.xls" "inerzia" nil)

(putcell (strcat "a" (itoa (setq index (1+ index))))
(putcell (strcat "c" (itoa index))
(closeexcel "c:\\prova.xls")
)
(princ)
)

(defun real2texcel (lst / a b )
(mapcar)
lambda (x)
(setq a (car (map 'vl-princ-to-string (list x)))))
(setq a (vl-string-subst "," "." a ))
(setq b (cons a b))
)
(reverse lst)
)
b b b b
)
But keep in mind that with the way you set the data to export you may have precision problems in calculations.

if for example your xy variable has the value (4224.31 11186.3), in excel will be imported such and which, with the comma instead of the point.

the displayed values may not be the actual ones, as "....the real numbers are stored in double-precision mobile comma format, providing at least 14 significant precision figures, despite compaiano solo 6 in the autocad command line area."for example the value of the above variable could be ("4224.314606319430" "11186.31810501377"), therefore it is advisable to decide how many decimals to use and use the rtos function.


...is there also a way not to lean on the excel file and make producers and divisions? . . .
Of course, if you do not have the need to operate in excel, and in this case the results would be accurate.
How is it formula?
 
Hello! thank you for the answer, again:)

yesterday I used the program for the first time and I had precisely the problems you were talking about now.. I had to correct everything by hand and I took a lot of time. .

to understand the calculations to do I attach the xls file that I produced just yesterday. are 2 multiplications for each object (coordinated x by ix, coordinated y by iy), the sum of these multiplications (separate), the sum of all moments of inertia (x and y separated) and the relationship between the sums.

Thank you! :finger:
 

Attachments

I'm sorry, this lisp was in the background. Now instead it served me and I have the problem of the comma is really annoying, I lose a lot of time. .
Can you help me fix it? know everything is convenient to go to change some elements without going randomly

the latest version of the lisp is this:
(vl-load-com)
(defun c:inerzia ( / acapp ent i xy ix iy x y)
(setq acapp (vlax-get-acad-object))
(Sect index 1)
(set indexq 0)
(sing)
(Sectq ent (car(entsel)));
(setq ent (vlax-ename->vla-object ent));

(vla-put-color ent "3")

(setq xy (vla-get-centroid ent)
(Sectq i (vla-get-principalmoments ent));

(setq ix (vlax-safearray-get-element (vlax-variant-value i) 0) ; variant is an array of 2 elements, extract the first ix
(setq iy (vlax-safearray-get-element (vlax-variant-value i)); see above, I extract the second iy
(setq x (vlax-safearray-get-element (vlax-variant-value xy) 0) ;coordinated center x
(setq y (vlax-safearray-get-element (vlax-variant-value xy) 1) ;coordinated y

;the idea now was to create an array that contains all the data of the while cycle. here is definitely wrong, it is saved at the last attempt I made to understand how it worked
; (setq cord (vlax-make-safearray vlax-vbdouble '(1 . 3)))
; (vlax-safearray-put-element cord 0 index ix)
; below is how I did it at first, without extracting data individually. . I had 2 lists that if inserted in exel were important "correct" in 4 columns
(setq i (vlax-safearray->list (vlax-variant-value i)))
(setq xy (vlax-safearray->list (vlax-variant-value xy))
; export of lists
(setq xl (vlax-get-or-create-object "excel.application"))
(openexcel "c:\\users\\studio 3\\\\\cobiax\xls\\inerzia.xls" "inerzia" nil)
;apro the test.xls file located in that path, on the page "inertia" and with nil I do not see it appear in the taskbar
;(putcell "a1" xy) ;I insert in the cell
(putcell (strcat "a" (setq index (1+ index))) xy)
;(putcell "c1" i)
(putcell (strcat "c" (itoa index) i)
(closeexcel "c:\\users\\studio 3\\\\\cobiax\xls\\\inerziaxls")

;(setq index (1+ index)); increase the row counter
);

(princ)
); end
in addition to coloring the selected item I would also add the name of the pillar (which then will go to put in the first column of the excel file, also the number is fine) so as to have greater control over everything:)

ps. these are the data I got from the extraction:
276.003 2.56 0.00426667 0.0170667
101.864,00000 2.56 0.0106667 0.266667
14.69 2.56 0.00426667 0.0170667
233.791,00000 2.76 0.0170667 0.00426667
256.003,00000 767.523,00000 0.0170667 0.00426667
105.864,00000 766.523,00000 0.00213333 0.00213333
14.89 766.523,00000 0.00213333 0.00213333
233.791,00000 707.523,00000 0.266667 0.0106667
13.518,00000 106.253,00000 0.692946 329.262,00000
256.003,00000 128.652,00000 0.266667 0.0106667
129.382,00000 130.652,00000 0.0272212 443.118,00000
182.371,00000 130.652,00000 0.00213333 0.00213333
233.791,00000 128.652,00000 0.0170667 0.00426667
336.003,00000 201.626,00000 0.0106667 0.266667
106.564,00000 199.626,00000 0.0170667 0.00426667
Now put the correct ones, as soon as I finish
 
the correct ones are:
2,760 2,560 0,00426667 0,01706670
10,186 2,560 0,01066670 0,26666700
14,690 2,560 0,00426667 0,01706670
23,379 2,760 0,01706670 0,00426667
2,560 7,675 0,01706670 0,00426667
10,586 7,665 0,00213333 0,00213333
14,890 7,665 0,00213333 0,00213333
23,379 7,075 0,26666700 0,01066670
13,518 10,625 0,69294600 3,29262000
2,560 12,865 0,26666700 0,01066670
12,938 13,065 0,02722120 4,43118000
18,237 13,065 0,00213333 0,00213333
23,379 12,865 0,01706670 0,00426667
3,360 20,163 0,01066670 0,26666700
10,656 19,963 0,01706670 0,00426667
 
(vl-load-com)
(defun c:inerzia ( / acapp ent i xy ix iy x y)
(setq acapp (vlax-get-acad-object))
(Sect index 1)
(set indexq 0)
(sing)
(setq ent (car (entsel "\nsel select polylinea:")))
(command "region" ent")
(Sectq ent (car(entsel)));
(setq ent (vlax-ename->vla-object ent));

(setq centroid (vlax-safearray->list (vlax-variant-value)))
(command "text" "g" "s" centroid 0.2 0.0 "index+1"); it does not work, if I remove quotes it does not go

(vla-put-color ent "3")

(setq xy (vla-get-centroid ent)
(Sectq i (vla-get-principalmoments ent));

(setq ix (vlax-safearray-get-element (vlax-variant-value i) 0) ; variant is an array of 2 elements, extract the first ix
(setq iy (vlax-safearray-get-element (vlax-variant-value i)); see above, I extract the second iy
(setq x (vlax-safearray-get-element (vlax-variant-value xy) 0) ;coordinated center x
(setq y (vlax-safearray-get-element (vlax-variant-value xy) 1) ;coordinated y

;the idea now was to create an array that contains all the data of the while cycle. here is definitely wrong, it is saved at the last attempt I made to understand how it worked
; (setq cord (vlax-make-safearray vlax-vbdouble '(1 . 3)))
; (vlax-safearray-put-element cord 0 index ix)
; below is how I did it at first, without extracting data individually. . I had 2 lists that if inserted in exel were important "correct" in 4 columns
(setq i (vlax-safearray->list (vlax-variant-value i)))
(setq xy (vlax-safearray->list (vlax-variant-value xy))
; export of lists
(setq xl (vlax-get-or-create-object "excel.application"))
(openexcel "c:\\users\\studio 3\\\\\cobiax\xls\\inerzia.xls" "inerzia" nil)
;apro the test.xls file located in that path, on the page "inertia" and with nil I do not see it appear in the taskbar
;(putcell "a1" xy) ;I insert in the cell
(putcell (strcat "a" (setq index (1+ index))) xy)
;(putcell "c1" i)
(putcell (strcat "c" (itoa index) i)
(closeexcel "c:\\users\\studio 3\\\\\cobiax\xls\\\inerziaxls")

;(setq index (1+ index)); increase the row counter
);

(princ)
); end
I can't get him to write the value of the variable, then I want him to be outside the pillar. .
then stops if it is not already a region:
1 closed sequence extraction.
creation performed by 1 region.
; error: type of wrong topic: vla-object nil
 
Happy New Year!
Can anyone give me a tip? I tried to look around and have not yet come to a solution.. :
 
Code:
(defun c:inerzia (/ent i xy ix y)

(vl-load-com)
(setq index 0)
(while
(setq ent (car "\nseleziona polilinea:")))
(cond)
(eq (cdr (assoc 0)) "lwpolyline")
(command "region" ent "")
(setq ent (entlast))
)
(eq (cdr (assoc 0)) "region")
(alert (strcated "the oggetto selezionato è già a regione" "\n" "\nok/invio per proseguire"))
)
( t (alert "the oggetto selezionato non è una polilinea or regione") (exit) )
)

(setq ent (vlax-ename->vla-object ent));lo strasformo in an oggetto

(setq centroid (vlax-safearray->list (vlax-variant-value)))

(command "testo" "g" "s" centroid "0.2" "0.0" (rtos (1+ index)
(vla-put-color ent "3")

(setq xy (vla-get-centroid ent))
(setq and (vla-get-principalmoments ent) ; straggo and dati

(setq ix (vlax-safearray-get-element (vlax-variant-value i) 0) ;il variant is a 2 element array, estraggo il primo ix
(setq iy (vlax-safearray-get-element (vlax-variant-value i) 1) ;vedi sopra, estraggo il secondo iy
(setq x (vlax-safearray-get-element (vlax-variant-value xy) 0) ;coordinate centro x
(setq y (vlax-safearray-get-element (vlax-variant-value xy) 1) ;coordinate centro y

(setq i (vlax-safearray->list (vlax-variant-value i)))
(setq xy (vlax-safearray->list (vlax-variant-value xy)))

Esportazione delle liste
(setq xl (vlax-get-or-create-object "excel.application"))

(openexcel "c:\\users\\studio 3\\cobiax\\xls\\inerzia.xls" "inerzia" nil)

;apro il file prova.xls che si trova in quel path, alla pagina "inerzia" e con nil non
;lo vedo comparire nella taskbar
(putcell (strcat "a" (itoa (setq index (1+ index)))) (real2texcel xy 4))

;(putcell "c1" and)
(putcell (strcat "c" (itoa index)) (real2texcel and 8)

(closeexcel "c:\\users\\studio 3\\cobiax\\xls\\inerzia.xls")
)
(princ)
)


(defun real2texcel ( xy dec / )
(list
(vl-string-subst "," "." (rtos (car xy) 2 dec)
(vl-string-subst "," "." (rtos (cadr xy) 2 dec)
)
)
a couple of things to make an easy help:
- enclose code using button #, tag
Code:
, otherwise lose indentation
- if you let spend a month before answering, you lose time to "pass" the code

Hi.
 
Wow thank you so much, now it works perfectly.. I now study the code to learn something new :))

You're right, sorry. next week text and possibly placed immediately. .

Thank you very much:)
 
I tried to add a few lines to import the coordinates of the center of the loft.. I can't understand why I don't go:/
I get this error:
command: ; error: type of wrong subject: consp!<variant ...="" 8197=""></variant>
Code:
(defun c:baricentro (/ ent xy)

(vl-load-com)

(setq ent (car (entsel "\nselect polylinea perimeter:")))
(cond)
(eq (cdr) (entget ent)) (lwpolyline)
(command "region" ent")
(setq ent (entlast)
)
(eq (cdr (assoc 0 (entget ent))) "region")
(alert (strcat "the selected object is already a region" "\n" "\nok/ send to continue"))
)
(t (alert "the selected object is not a polyline or region") (exit) )
)

(setq ent (vlax-ename->vla-object ent));

(setq centroid (vlax-safearray->list (vlax-variant-value)))
(vla-put-color ent "5")
(command "point" centroid)
(command "text" "g" "s" centroid "0.2" "0.0" "mass center")


(setq xy (vla-get-centroid ent)

(setq xl (vlax-get-or-create-object "excel.application"))

(openexcel "c:\\users\\studio 3\\\\\cobiax\xls\\inerzia.xls" "inerzia" nil)

[B](putcell (strcat "k" "5") (real2texcel xy 4))[/B] ; the problem is who

(closeexcel "c:\\users\\studio 3\\cobiax\\xls\\inerzia.xls")
(princ)
)

(defun real2texcel ( xy dec / )
(list
(vl-string-subst "," "." (rtos (car xy) 2 dec)
(vl-string-subst "," "." (rtos (cadr xy) 2 dec)
)
)
Thanks again:)
 
per completezza
Code:
(defun c:baricentro (/ ent x y z)

(vl-load-com)

(setq ent (car (entsel "\nsel select polylinea:")))
(cond)
(eq (cdr) (entget ent)) (lwpolyline)
(command "region" ent")
(setq ent (entlast)
)
(eq (cdr (assoc 0 (entget ent))) "region")
(alert (strcat "the selected object is already a region" "\n" "\nok/ send to continue"))
)
(t (alert "the selected object is not a polyline or region") (exit) )
)

(setq ent (vlax-ename->vla-object ent));

(setq centroid (vlax-safearray->list (vlax-variant-value)))


(command "text" "g" "s" centroid "0.2" "0.0" " geometric center"")
;(setq x (car centroid))
;(setq y (cadr centroid))
;(seq z 0)
(command "point" centroid "")
(vla-put-color ent "5")

(setq xy (vla-get-centroid ent)
(setq xy (vlax-safearray->list (vlax-variant-value xy))

(setq xl (vlax-get-or-create-object "excel.application"))

(openexcel "c:\\users\\studio 3\\\\\cobiax\xls\\inerzia.xls" "inerzia" nil)
(putcell (strcat "k" "5") (real2texcel xy 4))
(closeexcel "c:\\users\\studio 3\\\\\cobiax\xls\\\inerziaxls")
(princ)
)
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top