Jump to content

Get Mass Properties of a region and update exisitng text box


atr140

Recommended Posts

I am very new to using lisp routines, so please excuse my ignorance.

I have noticed that I have been using AutoCAD to get the sectional properties of built up sections quite often. To do this, I generally draw the shape, make it a region, and use massprop to get the centroid, area, moment of inertia, etc of the shape.

 

I would then copy and paste the info onto a titleblock along with the shape, and manually enter the centroid (place a point and change the point style in order to easily see it once it is printed)

 

I know there is an easier way, but again I am a newbie. After searching a bit, I found a way to draw a point at the centroid of a region.

 


(defun c:gecen(/ cSetcLst oldSnp)
(vl-load-com)
(princ "\n<<<Select Regions or 3D-solids >>> ")
(if(setq cSet(ssget '((0 . "REGION,3DSOLID"))))
(progn
(setq cLst(mapcar 'vlax-ename->vla-object
(vl-remove-if 'listp
(mapcar 'cadr(ssnamex cSet))))
oldSnp(getvar "OSMODE")
); end setq
(mapcar 'setvar (list "OSMODE" "CMDECHO")(list 0 0))
(foreach ent cLst
(if(vlax-property-available-p ent 'Centroid)
(command "_.point"(vlax-get ent 'Centroid))
); end if
); end foreach
(mapcar 'setvar (list "OSMODE" "CMDECHO")(list oldSnp 1))
); end progn
); end if
(princ)
); end of c:gecen

 

What I am missing is grabbing the sectional properties information (area, centroid, moment of inertia, etc) and putting it in a textbox. Ideally, I would like to update a pre-existing textbox set in paperspace on titleblock. I could then fit the shape into a window on paperspace and print out all of the information.

 

I appreciate any and all help on this matter! Thank you in advance!

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

Firstly welcome to CADTutor .

 

Secondly most of what you are doing could be done with a lisp program , so it is better to give an example with a drawing showing BEFORE and AFTER to allow users to get your point very clearly and to write a program in one shot :D

Edited by Tharwat
typo
Link to comment
Share on other sites

Tharwat,

 

Thanks for the reply and suggestion!

 

Please see attached for my vision :)

 

I would like to be able to draw a polygon, and run one command that will draw a point at the centroid, and fill in the "blanks" of all the sectional properties. I know that in order to use massprop (as i have done in the past) the polygon must be a region and the bottom left should be at (0,0). If this could be done without converting it to a region, and to make the centroid relative to the object that would be great! Please let me know if any other clarification is needed! Thanks for the help

 

Aaron

 

Here is the example...

 

http://www.filedropper.com/example_9

Link to comment
Share on other sites

One question at the mean time , how would you draw the polygon ? do you have a list of points or just by the command pline ?

 

It is late here for now , so tomorrow morning will continue after you answer the above mentioned question(s) . :)

Link to comment
Share on other sites

I couldn't download the file , upload it here by going to button Go Advanced which is on the bottom right side hand of this page and press attach to collect the required sample drawing .

Link to comment
Share on other sites

No worries , after many times of trying I could have downloaded the drawing :)

 

I see that you have the polyline drawing in Model space and the Mtext is in paper space ? although you don't want to select the Mtext to replace or add value because as it is clear from the drawing , you

want to place the values in order on each line that is on the right side of the Mtext object .

 

Suggestion : don't you think that it could be much better if you replaced the Mtext with Attributed Block ? furthermore that would be much more easier to fulfill the values in its correct location than the way you go now with Mtext .

Link to comment
Share on other sites

That is a great idea. I like the idea of setting up attributes. I will take some time later today to update that. Good suggestion!

Good stuff :thumbsup:

 

Can you explain what is the meaning of the last two chars in the Mtext object ( l J ) or what they represent ?

Link to comment
Share on other sites

The principal moments are the moments along the principal axes, with the

origin at the centroid.

 

They are related to the principal axes, rather than any other

coordinate system which might be defined. They

are characteristics of the object, in other words.

Link to comment
Share on other sites

Here we go now .

 

Process of the program :

 

You should select a CLOSED polyline and after that the program would search for the attributed block entitled SECPROP then write the value to each attributed text string in the block .

 

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 .

 

Try the program on your last attached drawing but you should pick a closed polyline as I have mentioned earlier .

 

Let me know how things going on with you .

 

Good luck :)

 

(defun c:Test  (/ cl o lst b)
;;;    --------------------------    ;;;
;;;    Author    : Tharwat Al Shoufi    ;;;
;;;    Date    : 05.February.2015    ;;;
;;;    --------------------------    ;;;
 (defun _reg  (o / r l e p d vls)
   (if (zerop (getvar 'PDMODE))
     (setvar 'PDMODE 34)
     )
   (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
           )
     )
   (if r
     (progn
       (entmake
         (list '(0 . "POINT")
               (cons 10 (setq p (vlax-get (car r) 'Centroid)))))
       (setq e   (car r)
             vls (mapcar
                   '(lambda (x)
                      (vlax-safearray->list (vlax-variant-value x)))
                   (list (vla-get-MomentOfInertia e)
                         (vla-get-radiiofgyration e)
                         (vla-get-PrincipalMoments e)
                         )
                   )
             l
                 (list (vla-get-area e)
                       (vla-get-perimeter e)
                       (car p)
                       (cadr p)
                       (caar vls)
                       (cadar vls)
                       (caadr vls)
                       (cadr (cadr vls))
                       (caaddr vls)
                       (cadr (caddr vls))
                       )
             )
       (if d
         (vla-delete e))
       )
     )
   l
   )

 (if
   (and
     (if (or (minusp (cdr (assoc 62
                                 (setq cl
                                        (entget
                                          (tblobjname "LAYER" (getvar 'CLAYER)))))))
             (= 4 (logand 4 (cdr (assoc 70 cl))))
             )
       (progn
         (alert "<!> Current Layer is either OFF or LOCKED <!>")
         nil)
       t
       )
     (if (not (setq b
                     (ssget "_X"
                            '((0 . "INSERT") (66 . 1) (2 . "SECPROP")))))
       (progn
         (alert
           "Couldn't find any attributed block entitled < SECPROP > !!")
         nil
         )
       t)
     (princ
       "\n Select CLOSED Object [REGION,CIRCLE,ELLIPSE,LWpolyline] :")
     (if (not (setq o (ssget "_+.:S:E"
                             '((-4 . "<OR")
                               (0 . "REGION,CIRCLE,ELLIPSE")
                               (-4 . "<AND")
                               (0 . "LWPOLYLINE")
                               (-4 . "&=")
                               (70 . 1)
                               (-4 . "AND>")
                               (-4 . "OR>")))))
       (progn
         (alert "\n << Invalid Object !! >>")
         nil)
       t
       )
     (setq lst (_reg o))
     )
    (mapcar '(lambda (x v)
               (if v
                 (vla-put-textstring x (rtos v 2 2))))
            (vlax-invoke
              (vlax-ename->vla-object (ssname b 0))
              'getattributes)
            (reverse lst)
            )
    )
 (princ)
 )(vl-load-com)

Edited by Tharwat
Program updated as been required
Link to comment
Share on other sites

Hi Tharwat,

 

I am following this thread with interest.

Just throwing some thoughts in here and I hope I am not treading on anybody's toes, but it may be better to make the target shape a region rather than a closed polyline.

I am thinking about when the target profile is a hollow box or tube.

I can see that you are following the OP's request. Just thinking that the occasion may crop up in the future.

 

Or....... Make an option to pick either a closed polyline or a region.

Link to comment
Share on other sites

Hi Tharwat,

I am following this thread with interest.

 

I am really happy that you interests in this thread .

 

Or....... Make an option to pick either a closed polyline or a region.

 

Yeah , I have thought of that but since that was the first draft of the program that I have posted , I wanted to know the OP's opinion then we may can expand the program to the limits we looking for and as much as

the experience can serves me . :D

Link to comment
Share on other sites

Yes Tharwat, I should really let the OP comment first.

I must say that I am already liking what you have produced so far.

 

Cheers and keep up the good work.

Link to comment
Share on other sites

I tried the program and it works great! I am very pleased. I do think I prefer the idea of being able to select a region or polyline. I do work with hollow tubes as well. Another thought, can we round all the values to 2 decimals?

 

Cheers to you!!! Good work!

Link to comment
Share on other sites

I tried the program and it works great! I am very pleased. ....

 

Cheers to you!!! Good work!

 

I am really happy to hear that , and you are welcome anytime .

 

I do think I prefer the idea of being able to select a region or polyline. I do work with hollow tubes as well.

 

As you know if you select a closed polyline or object a region would be created and delete at the end of the program and that is not visible to user to see , so do you want to delete the region if the user selected a region after all ?

 

can we round all the values to 2 decimals?

 

Sure we can .

Link to comment
Share on other sites

Anyway , I updated the program and added more options than the ones that have been required to become almost a watertight program . :D

 

Try it gentle men and let me know .

 

Good luck .

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