MOMENTS OF INERTIA

winpegasus
edited December 2022 in LISP
Hello guys,
There is a lisp attached that calculates the moment of inertia over region and pline objects. Related calculations appear on the command line. I tried for Attributes to settle into their place and come to the screen, but it didn't work. MASSPROP, which is Autocad's command for this job, only stays in region and solid objects, and its calculations remain on the command line. It's not very functional. I'm also open to information or guidance on the subject.

CODE:

(defun MODES (a)
(setq MLST '
())
(repeat (length a)
(setq MLST (append MLST (list (list (car a) (getvar (car a))))))
(setq a (cdr a))
)
)
;;; Restore modes

(defun MODER ()
(repeat (length MLST)
(setvar (caar MLST) (cadar MLST))
(setq MLST (cdr MLST))
)
)
;;; Ascii Text error handler

(defun at_err (st) ; If an error (such as CTRL-C) occurs
; while this command is active...
(if (/= st "Function cancelled")
(princ (strcat "\nError: " st))
)
(moder) ; Restore modified modes
(if (= (type rtfile) 'FILE)
(close rtfile)
)
(setq rtfile nil)
(setq *error* olderr) ; Restore old *error* handler
(princ)
)

;;; Read file line by line and store them

(defun readtxt1 (rf / rtfile txt txt1 txt2 txt3 txt4 txt5 txt6 txt7 txt8
txt11 txt12 txt13 txt 14 txt15 txt16 xcen ycen xmin ymin)
(setq rtfile (open rf "r"))
(setq txt (read-line rtfile)) ;- space file header
(setq txt1 (read-line rtfile) ;- type of entity
txt2 (read-line rtfile) ;- space
txt3 (read-line rtfile) ;- area
txt4 (read-line rtfile) ;- perimeter
txt5 (read-line rtfile) ;- Bounding box x
txt6 (read-line rtfile) ;- Bounding box y
txt7 (read-line rtfile) ;- centroid-X
txt8 (read-line rtfile) ;- centroid-Y
txt9 (read-line rtfile) ;- MOI x
txt10 (read-line rtfile) ;- MOI y
txt11 (read-line rtfile) ;- POI x & y
txt12 (read-line rtfile) ;- ROG x
txt13 (read-line rtfile) ;- ROG y
txt14 (read-line rtfile) ;- PM heading
txt15 (read-line rtfile) ;- PM I
txt16 (read-line rtfile) ;- PM J
) ; setq
(close rtfile)
(setq rtfile nil)

(setq xmax (nth 5 (read (strcat "(" txt5 ")")))
ymax (nth 3 (read (strcat "(" txt6 ")")))
xmax2 (nth 3 (read (strcat "(" txt5 ")")))
ymax2 (nth 1 (read (strcat "(" txt6 ")")))
xmin (nth 3 (read (strcat "(" txt5 ")")))
ymin (nth 1 (read (strcat "(" txt6 ")")))
xcen (nth 2 (read (strcat "(" txt7 ")")))
ycen (nth 1 (read (strcat "(" txt8 ")")))
xycen (list xcen ycen)
xstr (polar (list xmin ycen) (dtr 180) (* 5 h))
xend (polar (list xmax ycen) (dtr 0) (* 4 h))
ystr (polar (list xcen ymin) (dtr -90) (* 4 h))
yend (polar (list xcen ymax) (dtr 90) (* 4 h))
;;;; add weight
wht (* 0.00271 (nth 1 (read (strcat "(" txt3 ")" ))))
)
)

;;; Read file line by line and paste them

(defun readtxt2 (rf / s eof)
(setq rtfile (open rf "r"))
(setq eof nil)
(while (null eof)
(setq s (read-line rtfile))
(if s
(progn
(command "TEXT" pt1 h ang s)
(setq pt1 (polar pt1 (dtr -90) (* 1.666 h)))
)
(setq eof T)
);if
)
(close rtfile)
(setq rtfile nil)
)

;;; -------------------------- MAIN PROGRAM -------------------------

fun masstxt (ep1 / olderr ang eof rf h llea pt pt1 s xycen xstr xend
ystr yend xmax ymax i j Zx Zy s1 s2 txt9 txt10)

(setq olderr *error* *error* at_err)
(modes '("BLIPMODE" "CMDECHO" "HIGHLIGHT" "CLAYER"))
(setq rf (strcat (getvar "dwgname") ".mpr"))
;; Prompt for start point
(while (null pt)
(setq pt (getpoint "\nStart point : "))
(if (not pt) (princ " no point picked, try again"))
)
(setq pt1 pt) ; First insertion point

;;; set text style
(setq oldsty (getvar "textstyle"))
(if (= (tblsearch "STYLE" "MONOTXT") nil)
(command "style" "monotxt" "monotxt" "" "" "" "" "" "")
(setvar "textstyle" "monotxt")
); if monostyle

;; Prompt for a text height
(setvar "textsize" (* 2.75 (getvar "dimscale")))
;;; turn off by bong
;;; (setq h (getdist pt (strcat "\nHeight <"
;;; (rtos (getvar "TEXTSIZE")) ">: ")))
;;; (if (not h)
(setq h (getvar "TEXTSIZE"))
;;; )
(setq ang 0)
(setvar "BLIPMODE" 0)
(setvar "HIGHLIGHT" 0)
(setvar "CMDECHO" 0)

;*****************************************************************
(cond ( (and *debug* (/= 0 *debug*))
(setq *error* nil)
(setvar "cmdecho" 1)))
;*****************************************************************
;;; turn off by bong.
(if (= llea nil)
(command ".layer" "M" "massprop" "c" "3" "" "")
(command ".layer" "c" "3" "massprop" "")
)

(setvar "clayer" "massprop")
(readtxt1 rf)
(if (= (tblsearch "ltype" "center2") nil)
(command "linetype" "l" "center2" "acad" ""))
(setvar "cecolor" "9") (setvar "celtype" "center2")
(command ".line" xstr xend "" ".line" ystr yend "")
(setvar "cecolor" "BYLAYER") (setvar "celtype" "BYLAYER")
(command ".text" (polar xend (dtr 135) (* h 2)) (* h 1.5)"" "X")
(command ".text" (polar yend (dtr -45) (* h 2)) (* h 1.5)"" "Y")
(command ".copy" ep1 "" xycen "0,0")
(command "massprop" (entlast) "" "Y" (getvar "dwgname"))
(command ".erase" (entlast) "" ".redraw")
(readtxt1 rf)
(readtxt2 rf)

(setq i (nth 4 (read (strcat "(" txt9 ")")))
j (nth 1 (read (strcat "(" txt10 ")")))
Zx (/ i ymax)
Zy (/ j xmax)
Zx2 (/ i (abs ymax2))
Zy2 (/ j (abs xmax2))
s1 (strcat "Modulus of Elasticity:Zx1 = " (rtos Zx 2 0) " Zx2 = " (rtos Zx2 2 0))
s2 (strcat " Zy1 = " (rtos Zy 2 0) " Zy2 = " (rtos Zy2 2 0))
)

(command ".text" pt1 h ang s1)
(command ".text" (polar pt1 (dtr -90) (* 1.666 h)) h ang s2)
(command ".text" (polar pt1 (dtr -90) (* 3.32 h)) h ang (strcat "Estimated Weight (Alum): " (rtos wht) "kg/m" ))
(moder) ; Restore modified modes
(setq *error* olderr) ; Restore old *error* handler
(princ)
(setvar "textstyle" oldsty)
)

;;; COMMAND declaration

(defun c:masscal (/ ep1 prela preco) (vmon)
(setq ep1 nil)
(while (= ep1 nil)
(setq ep1 (entsel))
(if (null ep1) (princ "no object selected\n"))
)
(if (/= "REGION" (cdr (assoc 0 (entget (car ep1)))))
(progn
(setq prela (cdr (assoc 8 (entget (car ep1))))
preco (cdr (assoc 62 (entget (car ep1))))
)
(if (= preco nil) (setq preco "BYLAYER"))
(COMMAND "REGION" ep1 "")
(COMMAND "CHPROP" (entlast) "" "la" prela "c" preco "")
(setq ep1 (entlast))
)
(progn (princ "\n\tThe Object is REGION\n") (princ))
)
(COMMAND "MASSPROP" ep1 "" "Y" (getvar "dwgname"))
(masstxt ep1)(princ))
(dos_delete (strcat (getvar "dwgname") ".mpr"))

(defun C:MC ()(C:MASSCAL))
(princ "\n\ Extrussion Weight Added!")
(princ)

Comments

  • Not sure here in Bricscad but Massprop has an option write to file so you do that then read the file getting the line you want. Do in a lisp.