Jump to content

Get Mass Properties of a region and update exisitng text box


atr140

Recommended Posts

Nice code Tharwat. Works very well as it is currently intended.

You are very helpful and it's always good to see a happy OP. :)

 

 

I believe the code would also be useful to others to if the centroid could be 0,0

This would make the mass properties more meaningful.

 

NOTE: the program would write the values ONLY to one BLOCK that would be found , so if you have any plans to select the fore-said block , it is simple to revise the program accordingly .

I think the above adjustment would also be most beneficial. It would, I assume, give the option to use the code on multiple different shapes all within the same drawing.

 

I am not a person who is au fait with lisp. I will however try and study your code to see if I can fathom out how at least some of it works.

There are a few things that if added would improve the end result of the code very much but I am not able to easily judge if the amendments are simple or not.

Link to comment
Share on other sites

  • Replies 66
  • Created
  • Last Reply

Top Posters In This Topic

  • Tharwat

    26

  • Manila Wolf

    21

  • arkizner

    11

  • atr140

    8

Top Posters In This Topic

Posted Images

Nice code Tharwat. Works very well as it is currently intended.

You are very helpful and it's always good to see a happy OP. :)

 

Thank you , happy to hear that :)

 

 

I believe the code would also be useful to others to if the centroid could be 0,0

This would make the mass properties more meaningful.

 

I think the above adjustment would also be most beneficial. It would, I assume, give the option to use the code on multiple different shapes all within the same drawing.

 

Does it mean that you just want to keep the value of the centroid coordinates as an origin only all the time in the its related place in the Attributed Block ?

 

What are these the other shapes that you would like to add to the selection set of the program ? cause I am not that experienced in this kind of work .

 

I just explored the properties of the objects that the OP's asked about and with the experience in programming I could have wrote something useful in this regard , that's it .

Link to comment
Share on other sites

Hi Tharwat. Sorry, I did not explain very well.

 

I will try to explain better.

 

Regarding the centroid at origin 0,0: -

For the property readouts to read better, the centroid should be at origin 0,0,0

There are two ways of achieving this manually: -

Either move the whole shape and the centroid so that the centroid is placed at coordinates 0,0,0, then run the code a second time.

or

Position the UCS at the centroid then run the code a second time.

I was thinking that the code could place the UCS on the centroid, then extract the required info, populate the block then restore the UCS back to where it was.

 

Regarding the other shapes, I realise that this is not to do with the OP's request, but what I meant was that you may have several different shapes in one drawing that you may want to find the structural properties of each, individually, without overriding the figures in the block. Separate readouts in one drawing as such.

 

I hope this explains a little better.

 

Cheers Tharwat. Thanks for your good work always.

Link to comment
Share on other sites

Hi Tharwat, I am still looking for your help if you don't mind.

 

To get the code to give more meaningful readouts (since the values of the momentofinertia property are expressed relative to the current UCS), would it be something relatively simple like puttuing this near the start of the code: -

 

(command "_.ucs" "_origin" (vlax-get r 'centroid))

 

Then this somewhere at the end of the code: -

 

(command "_.ucs" "_p")

Link to comment
Share on other sites

Try this program and let me know :) .

 

Note: The program enable you to write the data into as many as you select of Attributed Blocks ( if you wanted to ) .

 

To be updated

Edited by Tharwat
Link to comment
Share on other sites

Hi Tharwat,

 

Thank you for your efforts in revising the code.

The ability to select the block works well.

 

The properties however are still not reading correctly.

I attach a lisp below that calculates the mass properties correctly by ensuring the centroid is at 0,0,0 before applying the standard AutoCAD massprop command.

This way the moment of inertia readouts are correct.

 

I wonder if your code could match the same results as this lisp.

 

Many Thanks.

 

;http://forums.augi.com/showthread.php?14840-MassProp-Moment-of-Inertia-J-Centroid
;kennet.sjoberg
(defun c:MassP (/ Ent Obj )
 (setq Ent (entsel "\nCommand: Select ONE 3D-solid or ONE 2D-region : " ) )
 (if Ent
   (progn
     (if (or (= (cdr (assoc 0 (entget (car Ent )))) "REGION" ) (= (cdr (assoc 0 (entget (car Ent )))) "3DSOLID" ) )
       (progn
         (vl-load-com)
         (setq Obj (vlax-ename->vla-Object (car Ent )) )
         (command "._ucs" "o" (vlax-get Obj "Centroid" ) )
         (command "._massprop" Ent "" "N" )
         (command "._ucs" "p" )
         (textscr)
       )
       (princ "Wrong object type !" )
     )
   )
   (princ "Miss, aim better !" )
 )
 (princ)
)

Link to comment
Share on other sites

Hi Tharwat, full marks for trying. Thank you for you time, I very much appreciate it.

 

Whilst not quite as efficient as populating the attributes in a block, I can go back to using the lisp below. (I got good help from pBe with this).

This pastes the section properties into the drawing as single line text. I can add the required units within the code.

This does position the UCS on the centroid, runs the massprop command, then sends the UCS back to where it came from.

 

You would have to create Make a folder C:/temp for this to work.

 

; Lots of help By pBe. Cadtutor Poster.
; http://www.cadtutor.net/forum/showthread.php?63907
; Modified by Mark.
;
; CALCULATION OF MASS PROPERTIES (Note: Make a folder C:/temp)
;
(defun c:MPmmTest (/ ss ptx fn fn1 str)
(setq lup (getvar "luprec"))
(setvar "luprec" 2)
 (defun Text (pt hgt str sty)
   (entmakex (list (cons 0 "TEXT")
     (cons 10 pt)
     (cons 40 hgt)
     (cons 7 sty) 
     (cons 1 str)
      )
   )
 )
 (setq txtlst nil)
 (if (setq ss (ssget '((0 . "REGION"))))
   (progn
     (command "_.UCS"
       "_Origin"
        (setq ptx (vlax-safearray->List
     (variant-value
       (vlax-get-property
         (vlax-ename->vla-object
    (cadar (ssnamex ss))
         )
         'centroid
       )
     )
   )
 )
     )
     (command "point" "0,0,0")
     (command "massprop"
       ss
       ""
       "y"
       (setq fn
       "c:/temp/mass.mpr"
       )
     )
     (terpri)
     (command "ucs" "w")
     (progn
(setq fn1 (open fn "r"))
(read-line fn1)
(while
  (setq str (read-line fn1))
   (setq txtlst (cons str txtlst))
)
(close fn1)
     )
     (setq Txtpt (list (car ptx)
  (- (cadr ptx) (* (getvar 'Textsize) 1.70) )
  (last ptx)
   )
     )
     (mapcar (function (lambda (lst1 lst2)
    (Text (trans (setq Txtpt
          (list
     (car Txtpt)
     (- (cadr Txtpt)
        (* (getvar 'Textsize)
           1.70
        )
     )
     (last Txtpt)
          )
   ) 1 0)
   (getvar 'Textsize)
   (strcat lst1 lst2)(getvar 'Textstyle)
    )
  )
      )
      (reverse txtlst)
      '(""  ""   " mm2"  " mm"    " mm"    " mm"
 " mm"  " mm"   " mm4"  " mm4"  " mm2"  " mm"
 " mm"  ""   " mm4"  " mm4"
       )
     )
   )
 )
(setvar "luprec" lup)
 (princ)
)

Link to comment
Share on other sites

Hi ,

 

That's different than your goal of the routine as you have asked for .

To use the command massprop would help to retrieve the needed properties but the main struggling issue is that the outcome of it that it needs many extra lines of codes to re-arrange the text strings in the required shape to be written in the selected Attributed Blocks .

 

Anyway , I will not promise you with a complete program , but I may write something useful in this regard later on .

Link to comment
Share on other sites

Hi Tharwat,

You are so quick with your response. :)

 

I will find time to have a close look at this hopefully later today.

on first glance it appears that the UCS on the origin is indeed fixed. Thumbs up! :)

 

I may just need some time to evaluate how the attributes are populated. I mean the order in which they are populated.

 

I will of course get back to you.

 

Many thanks.

Link to comment
Share on other sites

Hi Tharwat,

 

I did get chance to have a more detailed look at the code.

It seems that it is not quite getting the order of the attributes in correct sequence.

I can see from the code that it searches the lines of the text thrown up in the .mpr file.

 

If you are inclined to look at the code further, I should say that my requirements as to which is the most useful information to be made into attributes are slightly different to the OP's. I was going to explain this earlier but you were lightening fast with your revised code. :)

 

Bit difficult to explain everything, so I made a drawing and attached it here.

 

Maybe you could comment first as to the feasibilty of it.

 

Where I am showing that formula's are required I believe that this could be incorporated in the lisp code. If formula's can not be incorporated, is it possible just to leave those attributes blank?

 

Hope I am not asking too much. I always worry that I am.

 

Many thanks.

MassPropsExample.dwg

Link to comment
Share on other sites

Oh no , I did expected this intersection of Block's in sequence of information but since that you did not raise that issue , I thought your block would be the same as the OP's block in this thread .

 

I need to know how or where did you bring the values of Section Modulus Zx & Zy plus the weight value ?

 

I need these information to include them in values that would be written to your attributed block .

 

NOTE: this kind of work is out of my specialization so that's why you need to explain your target of the lisp clearly , but anyway I guess that we have almost reached the seashore except one push to step on the land :D

Regards

Link to comment
Share on other sites

Yes this is rather a special set of requests. I do need to try and explain very clearly so no abortive work is done by your good self.

I will try and explain further.

 

 

First the Section Modulus: -

The output I am looking for is general only. There are many more complicated calculations that structural engineers use, but I am only interested in a general calculation.

(I am not a structural engineer by the way, I am a designer).

AutoCAD will not give you the section modulus readout directly so some simple maths are required.

 

To manually get the section modulus we call Zx (called Sx in the USA): -

From the standard AutoCAD massprop readouts, take the moment of inertia X figure and divide it by the BIGGEST of the two bounding box Y values.

 

To manually get the section modulus we call Zy (called Sy in the USA): -

From the standard AutoCAD massprop readouts, take the moment of inertia Y figure and divide it by the BIGGEST of the two bounding box X values.

 

Maybe can use the vla GetBoundingBox method to get biggest bounding box value? (Not entirely sure what I am talking about here)

 

 

For weight: -

For the code I am assuming units as millimetres and that the material is extruded aluminium.

To manually arrive at the weight in kilograms per metre, I need to multiply the area by the density of aluminium.

From the standard AutoCAD massprop readouts, take the Area and multiply by 0.00271

Since 0.00271 is the density the figure in the code will always be 0.00271. Only the Area is the variable.

 

 

If by chance these formula's can not be included in the lisp code, then I need the attribute value to be blank or untouched by the code. That way I think I can incorporate the formula's in the attribute block itself.

 

Phew!

Seems a bit complicated Tharwat.

 

I would be happy to explain more if needed.

Link to comment
Share on other sites

Phew!

Seems a bit complicated Tharwat.

 

No at all , you did explain it very clearly and I just need one dozen of minutes to revise and add the new values & calculations to the program .

 

Have a good day till then .

Link to comment
Share on other sites

Hi Tharwat,

 

I just corrected a couple of silly typo's in my drawing. Yikes! :cry:

I suspect it won't have affected what you are currently doing as it's a minor thing.

Sorry about that.

 

Just a thought, if it's easier for you I am quite happy that only regions are able to be selected like the massprop command. Especially if this means that the selected objects will be highlighted.

I will leave that one entirely up to you.

 

Cheers and Thanks.

MassPropsExample-RevA.dwg

Link to comment
Share on other sites

Okay now .

 

I did work on the program for a quite sometime that should work on very well with your new requirements besides that , with Attributed block(s) that have the same tag names as you did have in your block .

 

I also added an option to undo if you wanted to .

 

NOTE: Hope you don't remove the author name in the program .

 

Try it and let me know how things going on with you .

 

 

(defun c:WriteMass (/ *error* _get _doc cho f cl o b l _l _l1 _l2 ar srt fnd)
;;; -------------------------------------------------    ;;;
;;;    ---=== { Author : Tharwat Al Shoufi } ===---      ;;;
;;;                                                      ;;;
;;;     Write mass properties to Attributed Block(s)     ;;;
;;; -------------------------------------------------    ;;;
 (setq _doc (vla-get-activedocument (vlax-get-acad-object)))
 (defun *error* (x)
   (if (and f (setq f (findfile f)))
     (vl-file-delete f)
   )
   (if cho
     (setvar 'cmdecho cho)
   )
   (if (and x
            (not (wcmatch (strcase x) "*BREAK*,*EXIT*,*CANCEL*"))
       )
     (princ "Error:" x "...")
   )
 )
;;;                                ;;;
 (defun _get (f / of s lst l)
   (if (and (setq of (open f "r"))
            (while
              (setq s (read-line of))
               (setq lst (cons s lst))
            )
       )
     (progn
       (close of)
       (if (setq lst (reverse lst))
         (mapcar '(lambda (i) (setq l (cons (nth i lst) l)))
                 '(3 4 5 6 9 10 12 13)
         )
       )
       (setq l (reverse l))
     )
   )
   l
 )
;;;                                ;;;
 (cond
   ((or (minusp (cdr (assoc 62
                            (setq cl
                                   (entget
                                     (tblobjname "LAYER" (getvar 'CLAYER))
                                   )
                            )
                     )
                )
        )
        (= 4 (logand 4 (cdr (assoc 70 cl))))
    )
    (alert "<!> Current Layer is either OFF or LOCKED <!>")
   )
   ((and
      (princ
        "\n Select CLOSED Object [REGION,CIRCLE,ELLIPSE,LWpolyline] :"
      )
      (not (setq o (ssget "_+.:S:E:L"
                          '((-4 . "<OR")
                            (0 . "REGION,CIRCLE,ELLIPSE")
                            (-4 . "<AND")
                            (0 . "LWPOLYLINE")
                            (-4 . "&=")
                            (70 . 1)
                            (-4 . "AND>")
                            (-4 . "OR>")
                           )
                   )
           )
      )
    )
    (alert
      "\n << nil or Invalid Object or on Locked Layer !! >>"
    )
   )
   ((and (princ "\n Select Attributed Block(s) :")
         (not (setq b (ssget "_:L" '((0 . "INSERT") (66 . 1)))))
    )
    (alert "Invalid object . Should be Attributed Block(s) !")
   )
 )
 (if (and b
          (if (not (setq f (vl-filename-mktemp nil nil ".mpr")))
            (alert "Can't create the text file !!")
            t
          )
     )
   (progn
     (if (eq (cdr (assoc 0 (entget (ssname o 0)))) "REGION")
       (setq r (list (vlax-ename->vla-object (ssname o 0))))
       (setq r (vl-catch-all-apply
                 'vlax-invoke
                 (list
                   (vla-get-block
                     (vla-get-activelayout
                       (vla-get-ActiveDocument
                         (vlax-get-acad-object)
                       )
                     )
                   )
                   'Addregion
                   (list (vlax-ename->vla-object (ssname o 0)))
                 )
               )
             d t
       )
     )
     (setq cho (getvar 'cmdecho))
     (setvar 'cmdecho 0)
     (command "_.ucs"
              "_Origin"
              (setq p (vlax-get (car r) 'Centroid))
     )
     (command "_.massprop"
              (vlax-vla-object->ename (car r))
              ""
              "y"
              (vl-string-translate "\\" "/" f)
     )
     (command "_.ucs" "w")
     (setvar 'cmdecho cho)
     (if d
       (vla-delete (car r))
     )
     (if (zerop (getvar 'PDMODE))
       (setvar 'PDMODE 34)
     )
     (entmake
       (list '(0 . "POINT")
             (cons 10 p)
       )
     )
     (if (setq l (_get f))
       (progn
         (setq _l1 (mapcar
                     '(lambda (s d / p a b c)
                        (setq p (vl-string-search d s)
                              a (vl-string-trim
                                  " "
                                  (substr s (+ p 3))
                                )
                              p (vl-string-search "--" a)
                              b (substr a 1 p)
                              c (vl-string-trim
                                  " "
                                  (substr a (+ p 3))
                                )
                              b (if (wcmatch b "-*")
                                  (substr b 2)
                                  b
                                )
                              c (if (wcmatch c "-*")
                                  (substr c 2)
                                  c
                                )
                        )
                        (mapcar '(lambda (x) (vl-string-trim " " x))
                                (list b c)
                        )
                      )
                     (list (nth 2 l) (nth 3 l))
                     '("X:" "Y:")
                   )
               _l2 (mapcar '(lambda (s d / p a)
                              (setq p (vl-string-search d s)
                                    a (vl-string-trim
                                        " "
                                        (substr (vl-string-trim
                                                  " "
                                                  (substr s (+ p 3))
                                                )
                                                1
                                                p
                                        )
                                      )
                              )
                              a
                            )
                           (list (nth 4 l)
                                 (nth 5 l)
                                 (nth 6 l)
                                 (nth 7 l)
                           )
                           '("X:" "Y:" "X:" "Y:")
                   )
               ar  (vl-string-trim " " (substr (car l) 6))
               srt (list
                     ar
                     (vl-string-trim " " (substr (cadr l) 12))
                     (rtos (max (read (caar _l1))
                                (read (cadar _l1))
                           )
                           2
                           3
                     )
                     (rtos (max (read (caadr _l1))
                                (read (cadadr _l1))
                           )
                           2
                           3
                     )
                     "0.0000"
                     "0.0000"
                     (car _l2)
                     (cadr _l2)
                     (rtos (/ (read (car _l2))
                              (max (read (caadr _l1))
                                   (read (cadadr _l1))
                              )
                           )
                           2
                           3
                     )
                     (rtos (/ (read (cadr _l2))
                              (max (read (caar _l1))
                                   (read (cadar _l1))
                              )
                           )
                           2
                           3
                     )
                     (caddr _l2)
                     (nth 3 _l2)
                     (rtos (* (read ar) 0.00271) 2 3)
                   )
               _l  (mapcar '(lambda (j k) (cons j k))
                           '("AREA" "PERIMETER" "BOUNDING_BOX_X_MAX"
                             "BOUNDING_BOX_Y_MAX" "CENTROID_X"
                             "CENTROID_Y" "MOMENT_OF_INERTIA_X"
                             "MOMENT_OF_INERTIA_Y" "SECTION_MODULUS_ZX"
                             "SECTION_MODULUS_ZY"
                             "RADIUS_OF_GYRATION_X"
                             "RADIUS_OF_GYRATION_Y" "WEIGHT"
                            )
                           srt
                   )
         )
         (vla-startUndomark _doc)
         ((lambda (n / sn)
            (while (setq sn (ssname b (setq n (1+ n))))
              (mapcar
                '(lambda (x)
                   (if (setq
                         fnd (assoc (vla-get-tagstring x)
                                    _l
                             )
                       )
                     (vla-put-textstring x (cdr fnd))
                   )
                 )
                (vlax-invoke
                  (vlax-ename->vla-object sn)
                  'getattributes
                )
              )
            )
          )
           -1
         )
         (vla-endundomark _doc)
       )
     )
   )
 )
 (terpri)
 (*error* nil)
 (princ "\nThis Program is written by Tharwat Al Shoufi .")
 (princ)
)
(vl-load-com)
(princ
 "\n** Type WriteMass to start.. Author: Tharwat Al Shoufi .*"
)
(princ)

Edited by Tharwat
Final release
Link to comment
Share on other sites

Hi Tharwat,

 

I gave the revised code a thorough test. It works very well. All the figures came out correct on a variety of shapes that I tested.

The only minor thing that happened a few times (not every time) is that a "textscr" window opened and stayed open before the code finished.

 

I am very pleased indeed and salute your coding expertise and your willingness to help me and many others on this forum.

I sincerely thank you very much.

 

Regarding the author name in the program I can assure you Tharwat that I would never remove the Authors name. The credit should remain with you always.

I make a habit of naming the author and the source of every code I get from the internet.

If you track back through this thread you will see that I have done this on the two codes by others that I posted.

 

I know you spent a lot of time and effort on this and I am very grateful.

 

Cheers my friend.

Link to comment
Share on other sites

Hi Tharwat,

 

I did just find an anomaly.

Please see the attached drawing.

 

In the attached drawing I drew and tested 4 shapes.

 

Shapes 3 and 4 are just fine.

 

Shapes 1 and 2 show hyphens in the text.

 

Maybe something to do with units or decimal places? Not quite sure.

 

The above for your reference.

 

Cheers.

MassPropsTest.dwg

Link to comment
Share on other sites

Hi,

 

Firstly I am happy that the program worked as you expected and thanks for ensuring me about the author credits , that is very nice of you to insist on .

 

Secondly , I knew that the hyphen may take a place cause the hard part of the program was to parse the strings in their correct values .

 

Anyway I think I need to write a function to parse the strings in more precise and strict manner , so I need quite sometime to write this function later on and you 'd be updated with the codes for sure .

 

Tharwat

Link to comment
Share on other sites

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