Jump to content

Recommended Posts

Posted

I have this very useful mass proprties lisp (mp.lsp). I have had this for many years and I can't remember where I sourced it from.

 


;
; CALCULATION OF MASS PROPERTIES (This won't work if C: drive is locked 
by your administrator)
;
(defun c:mp()
(setq fn 
"c:/mass.mpr")
(princ "\n Please select a region:")
(setq ss 
(ssget))
(command "massprop" ss "" "y" fn)
(setq fn1 (open fn 
"r"))
(setq x (read-line fn1))
(setq n 1)
(while (/= 
n 5)
 (setq x (read-line fn1))
 (setq y 
(read-line fn1))
 (setq n (+ n 1))
)
(close 
fn1)
(setq x1 (read (substr x 26)))
(setq y1 (read (substr y 
26)))
(setq pt1 (list x1 y1))
(command "point" 
pt1)
(command "ucs" "o" pt1)
(command "massprop" ss "" "y" 
fn)
(textscr)
(command "ucs" "w")
)

 

It creates a temporary text file with extension .mpr that can be opened with any text editor. It places the text file in C: drive.

What I am looking for is a lisp that can open the text file then add pre-defined text at the end of each line, then insert the revised text back into the drawing.

For example, this is a typical readout in the text file. It always has the same number of lines of text: -

 

 

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

Area: 2500.000

Perimeter: 200.000

Bounding box: X: -25.000 -- 25.000

Y: -25.000 -- 25.000

Centroid: X: 0.000

Y: 0.000

Moments of inertia: X: 520833.334

Y: 520833.334

Product of inertia: XY: 0.000

Radii of gyration: X: 14.434

Y: 14.434

Principal moments and X-Y directions about centroid:

I: 520833.333 along [1.000 0.000]

J: 520833.333 along [0.000 1.000]

 

 

 

This is an example of what I would like the final text to look like, with the units added as a suffix on each line: -

 

 

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

Area: 2500.000 mm^2

Perimeter: 200.000 mm

Bounding box: X: -25.000 -- 25.000 mm

Y: -25.000 -- 25.000 mm

Centroid: X: 0.000 mm

Y: 0.000 mm

Moments of inertia: X: 520833.334 mm^4

Y: 520833.334 mm^4

Product of inertia: XY: 0.000 mm^2

Radii of gyration: X: 14.434 mm

Y: 14.434 mm

Principal moments and X-Y directions about centroid:

I: 520833.333 along [1.000 0.000] mm^4

J: 520833.333 along [0.000 1.000] mm^4

 

 

I did search and can find lisp routines that apply a suffix on single text lines, one by one by typing manually for each line. But if I can automate it, so much the better.

 

Another hopeful post by me looking for help.

Posted

I did figure out a relatively quick and easy way to do this without lisp.

 

Open the .mpr file in Notepad, copy then paste the text into an excel file where the units can easily be appended to the text by Concatenation.

 

Copy the resultant text and paste special as text back into the AutoCAD dwg.

MassPropsExcel.jpg

Posted

Just read the lisp help on how to open file its very easy

I would

pick a pt

OPEN "r"

read a line in

add the extra text strcat

text at pt calculate next point 1.4 times the text height below car and cadr of pick a pt is x & y

 

 

 

The code is confusing seems to do the same thing twice but the read line is what you want .

Posted (edited)

 
(defun c:mp2 (/ ss ptx fn fn1 str)
 (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 "_+.:E:S" '((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:/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)
     )
     (textscr)
     (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)
      '(""  ""   " mm^2"  " mm"    " mm"    " mm"
 " mm"  " mm"   " mm^4"  " mm^4"  " mm^2"  " mm"
 " mm"  ""   " mm^4"  " mm^4"
       )
     )
   )
 )
 (princ)
)

Edited by pBe
include textstyle/change insertion point of text
Posted

pBeeeeeeeeeeeeeeeee It's brilliant. Works a treat. Thank you so much.

Yet again you have helped me considerably.

 

This code I will use regularly and even a none lisp guy like me can easily adapt the code to suit cm units and imperial units.

 

BIGAL, thanks for the comment. I think it motivated pBe to get stuck into this.

 

I really believe that many others all around the world will do a search and find and use this extremely useful code.

 

My own code: -

 

Cadtutor (pBe, Lee Mac, et al) = Fantastic Forum

Posted

 

BIGAL, thanks for the comment. I think it motivated pBe to get stuck into this.

 

 

You are welcome Manila Wolf, it was all BIGALS's idea really, it could have been written entirely different but i dont really want to re-write the whole code as a respect to the original author, but it works for you nevertheless.

 

Cheers

 

:beer:

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