Jump to content

Lisp lệnh HA trong chủ đề “Lisp tinh toan momen quan tinh xoan(J) cua tiet dien bat ky”

 

Tôi nhớ đã post cái này 1 lần, nhưng giờ quên link nên đành post lại. Hy vọng đúng ý bạn. Và nếu đúng thì xin nhận hậu tạ bằng LIKE cho gọn.

 

;Doan Van Ha - CADViet.com
;Tinh cac dac trung hinh hoc cua Polyline kin hoac Region.
(defun C:HA() 
 (setq obj (vlax-ename->vla-object (car (entsel "\Chon doi tuong kin (Polyline hoac Region): "))))
 (VxGetMassProps Obj))
(defun VxGetMassProps (Obj / DelFlg ResLst TmpObj)
 (or Gb:AcO (setq Gb:AcO (vlax-get-acad-object)))
 (or Gb:AcD (setq Gb:AcD (vla-get-activedocument Gb:AcO)))
 (if (member (vla-get-ObjectName Obj) '("AcDb2dPolyline" "AcDbPolyline"))
  (setq DelFlg T
        TmpObj (vlax-safearray-get-element  (vlax-variant-value (vla-AddRegion (vla-get-ModelSpace Gb:AcD) (VxListToArray (list Obj) vlax-vbObject))) 0))
  (setq TmpObj Obj))
 (setq ResLst (append
               (list
                (vlax-get TmpObj 'Centroid)
                (vlax-get TmpObj 'RadiiOfGyration)
                (setq a (vlax-get TmpObj 'PrincipalDirections))
                (vlax-get TmpObj 'PrincipalMoments)
                (vlax-get TmpObj 'MomentOfInertia))
               (if (= (vla-get-ObjectName TmpObj) "AcDbRegion")
                (list
                 (vla-get-ProductOfInertia TmpObj)
                 (vla-get-Area TmpObj)
                 (vla-get-Perimeter TmpObj))
                (list
                 (vlax-get TmpObj 'ProductOfInertia)
                 (vla-get-Volume TmpObj)
                 nil))))
 (if DelFlg (vla-delete TmpObj))
 (princ "\n\nKET QUA TINH: ")
 (princ "\n1). Centroid: ")
 (princ "\n X: ") (princ (car (nth 0 Reslst)))
 (princ "\n Y: ") (princ (cadr (nth 0 Reslst)))
 (princ "\n2). Radii Of Gyration: ")
 (princ "\n X: ") (princ (car (nth 1 Reslst)))
 (princ "\n Y: ") (princ (cadr (nth 1 Reslst)))
 (princ "\n3). Principal Directions: ")
 (princ "\n ") (princ (car (nth 2 Reslst)))
 (princ "\n ") (princ (cadr (nth 2 Reslst)))
 (princ "\n ") (princ (caddr (nth 2 Reslst)))
 (princ "\n ") (princ (nth 3 (nth 2 Reslst)))
 (princ "\n4). Principal Moments: ")
 (princ "\n I: ") (princ (car (nth 3 Reslst)))
 (princ "\n J: ") (princ (cadr (nth 3 Reslst)))
 (princ "\n5). Moment Of Inertia: ")
 (princ "\n X: ") (princ (car (nth 4 Reslst)))
 (princ "\n Y: ") (princ (cadr (nth 4 Reslst)))
 (princ "\n6). Product Of Inertia: ")
 (princ (nth 5 Reslst))
 (princ "\n7). Area: ")
 (princ (nth 6 Reslst))
 (princ "\n8). Perimeter: ")
 (princ (nth 7 Reslst))
 (textpage))
; (princ))
; ResLst=((70.5309 25.7049) (25.7963 70.5906) (0.966567 0.256415 -0.256415 0.966567) (285.635 561.65) (42933.0 321491.0) 116901.0 64.5172 37.4259)
;-----
(defun VxListToArray (Lst Typ)
 (vlax-make-variant (vlax-safearray-fill (vlax-make-safearray Typ (cons 0 (1- (length Lst)))) Lst)))
 

 

Em tò mò muốn anh Hà giải thích cho em hiểu sự khác biệt khi dùng lisp của anh so với dùng  lệnh MASSPROP:

 

Command: HA Chon doi tuong kin (Polyline hoac Region):

KET QUA TINH:

1). Centroid:

 X: 18.9681

 Y: 13.3743

2). Radii Of Gyration:

 X: 13.4322

 Y: 19.1217

3). Principal Directions:

 0.972257

 0.233915

 -0.233915

 0.972257

4). Principal Moments:

 I: 37.8715

 J: 179.868

5). Moment Of Inertia:

 X: 5309.66

 Y: 10760.3

6). Product Of Inertia: 7433.39

7). Area: 29.4288

8). Perimeter: 28.1659nil

Command:

 

Command: MASSPROP

Select objects: 1 found

Select objects:

 ----------------   REGIONS   ----------------

Area:                    29.4288

Perimeter:               28.1659

Bounding box:         X: 13.9725  --  23.4991

                      Y: 10.4037  --  16.9504

Centroid:             X: 18.9681

                      Y: 13.3743

Moments of inertia:   X: 5309.6579

                      Y: 10760.3003

Product of inertia:  XY: 7433.3925

Radii of gyration:    X: 13.4322

                      Y: 19.1217

Principal moments and X-Y directions about centroid:

                      I: 37.8715 along

                      J: 179.8684 along

Write analysis to a file? <N>: *Cancel*

Cách dùng

  1. Tải tệp 240801_ha.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh HA để chạy lisp.

Mã nguồn 240801_ha.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/73362-lisp-tinh-toan-momen-quan-tinh-xoanj-cua-tiet-dien-bat-ky/
;Doan Van Ha - CADViet.com
;Tinh cac dac trung hinh hoc cua Polyline kin hoac Region.
(defun C:HA() 
 (setq obj (vlax-ename->vla-object (car (entsel "\Chon doi tuong kin (Polyline hoac Region): "))))
 (VxGetMassProps Obj))
(defun VxGetMassProps (Obj / DelFlg ResLst TmpObj)
 (or Gb:AcO (setq Gb:AcO (vlax-get-acad-object)))
 (or Gb:AcD (setq Gb:AcD (vla-get-activedocument Gb:AcO)))
 (if (member (vla-get-ObjectName Obj) '("AcDb2dPolyline" "AcDbPolyline"))
  (setq DelFlg T
        TmpObj (vlax-safearray-get-element  (vlax-variant-value (vla-AddRegion (vla-get-ModelSpace Gb:AcD) (VxListToArray (list Obj) vlax-vbObject))) 0))
  (setq TmpObj Obj))
 (setq ResLst (append
               (list
                (vlax-get TmpObj 'Centroid)
                (vlax-get TmpObj 'RadiiOfGyration)
                (setq a (vlax-get TmpObj 'PrincipalDirections))
                (vlax-get TmpObj 'PrincipalMoments)
                (vlax-get TmpObj 'MomentOfInertia))
               (if (= (vla-get-ObjectName TmpObj) "AcDbRegion")
                (list
                 (vla-get-ProductOfInertia TmpObj)
                 (vla-get-Area TmpObj)
                 (vla-get-Perimeter TmpObj))
                (list
                 (vlax-get TmpObj 'ProductOfInertia)
                 (vla-get-Volume TmpObj)
                 nil))))
 (if DelFlg (vla-delete TmpObj))
 (princ "\n\nKET QUA TINH: ")
 (princ "\n1). Centroid: ")
 (princ "\n X: ") (princ (car (nth 0 Reslst)))
 (princ "\n Y: ") (princ (cadr (nth 0 Reslst)))
 (princ "\n2). Radii Of Gyration: ")
 (princ "\n X: ") (princ (car (nth 1 Reslst)))
 (princ "\n Y: ") (princ (cadr (nth 1 Reslst)))
 (princ "\n3). Principal Directions: ")
 (princ "\n ") (princ (car (nth 2 Reslst)))
 (princ "\n ") (princ (cadr (nth 2 Reslst)))
 (princ "\n ") (princ (caddr (nth 2 Reslst)))
 (princ "\n ") (princ (nth 3 (nth 2 Reslst)))
 (princ "\n4). Principal Moments: ")
 (princ "\n I: ") (princ (car (nth 3 Reslst)))
 (princ "\n J: ") (princ (cadr (nth 3 Reslst)))
 (princ "\n5). Moment Of Inertia: ")
 (princ "\n X: ") (princ (car (nth 4 Reslst)))
 (princ "\n Y: ") (princ (cadr (nth 4 Reslst)))
 (princ "\n6). Product Of Inertia: ")
 (princ (nth 5 Reslst))
 (princ "\n7). Area: ")
 (princ (nth 6 Reslst))
 (princ "\n8). Perimeter: ")
 (princ (nth 7 Reslst))
 (textpage))
; (princ))
; ResLst=((70.5309 25.7049) (25.7963 70.5906) (0.966567 0.256415 -0.256415 0.966567) (285.635 561.65) (42933.0 321491.0) 116901.0 64.5172 37.4259)
;-----
(defun VxListToArray (Lst Typ)
 (vlax-make-variant (vlax-safearray-fill (vlax-make-safearray Typ (cons 0 (1- (length Lst)))) Lst)))
 

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Lisp tinh toan momen quan tinh xoan(J) cua tiet dien bat ky. Xem đúng bài viết #240801 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 223)