Jump to content

lisp to calculate mass


naserrishehri@yahoo.com

Recommended Posts

Since the user is specifying the density value, simply enter a value in kg/m3 (i.e. 1000x larger) and change the t/m3 text accordingly - the calculation will be identical.

 

Try the following:

([color=BLUE]defun[/color] c:sp ( [color=BLUE]/[/color] den idx key obj sel vol )
   ([color=BLUE]setq[/color] key [color=MAROON]"LMac\\solprop-density"[/color])
   ([color=BLUE]if[/color] ([color=BLUE]or[/color] ([color=BLUE]setq[/color] den (getdensity key))
           ([color=BLUE]setq[/color] den (setdensity key))
       )
       ([color=BLUE]while[/color]
           ([color=BLUE]progn[/color]
               ([color=BLUE]setvar[/color] 'errno 0)
               ([color=BLUE]initget[/color] [color=MAROON]"Multiple Density Exit"[/color])
               ([color=BLUE]princ[/color] ([color=BLUE]strcat[/color] [color=MAROON]"\nCurrent density: "[/color] ([color=BLUE]rtos[/color] den 2) [color=MAROON]" kg/m³"[/color]))
               ([color=BLUE]setq[/color] sel ([color=BLUE]entsel[/color] [color=MAROON]"\nSelect edge of solid object [Multiple/Density/Exit]: "[/color]))
               ([color=BLUE]cond[/color]
                   (   ([color=BLUE]=[/color] 7 ([color=BLUE]getvar[/color] 'errno))
                       ([color=BLUE]princ[/color] [color=MAROON]"\nMissed, try again."[/color])
                   )
                   (   ([color=BLUE]or[/color] ([color=BLUE]null[/color] sel) ([color=BLUE]=[/color] [color=MAROON]"Exit"[/color] sel))
                       [color=BLUE]nil[/color]
                   )
                   (   ([color=BLUE]=[/color] [color=MAROON]"Density"[/color] sel)
                       ([color=BLUE]setq[/color] den (setdensity key))
                   )
                   (   ([color=BLUE]=[/color] [color=MAROON]"Multiple"[/color] sel)
                       ([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] '((0 . [color=MAROON]"3DSOLID"[/color]))))
                           (showdensity den
                               ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] idx ([color=BLUE]sslength[/color] sel))
                                   ([color=BLUE]setq[/color] vol
                                       ([color=BLUE]+[/color]  ([color=BLUE]cond[/color] (vol) (0.0))
                                           ([color=BLUE]vla-get-volume[/color] ([color=BLUE]vlax-ename->vla-object[/color] ([color=BLUE]ssname[/color] sel ([color=BLUE]setq[/color] idx ([color=BLUE]1-[/color] idx)))))
                                       )
                                   )
                               )
                           )
                       )
                   )
                   (   ([color=BLUE]not[/color] ([color=BLUE]vlax-property-available-p[/color] ([color=BLUE]setq[/color] obj ([color=BLUE]vlax-ename->vla-object[/color] ([color=BLUE]car[/color] sel))) 'volume))
                       ([color=BLUE]princ[/color] [color=MAROON]"\nSelected object does not have Volume property."[/color])
                   )
                   (   (showdensity den ([color=BLUE]vla-get-volume[/color] obj)))
               )
           )
       )
   )
   ([color=BLUE]princ[/color])
)
([color=BLUE]defun[/color] showdensity ( den vol )
   ([color=BLUE]setq[/color] vol ([color=BLUE]/[/color] vol 1e9))
   ([color=BLUE]alert[/color]
       ([color=BLUE]strcat[/color]
           [color=MAROON]"Properties for a solid object at "[/color]  ([color=BLUE]rtos[/color] den 2 2) [color=MAROON]" kg/m³ : \n\n"[/color]
           [color=MAROON]"Volume = "[/color] ([color=BLUE]rtos[/color] vol 2 2) [color=MAROON]" m³\t\t"[/color]
           [color=MAROON]"Weight = "[/color] ([color=BLUE]rtos[/color] ([color=BLUE]*[/color] den vol) 2 2) [color=MAROON]" kg"[/color]
       )
   )
)
([color=BLUE]defun[/color] getdensity ( key [color=BLUE]/[/color] den )
   ([color=BLUE]if[/color] ([color=BLUE]and[/color] ([color=BLUE]setq[/color] den ([color=BLUE]getenv[/color] key))
            ([color=BLUE]setq[/color] den ([color=BLUE]distof[/color] den 2))
            ([color=BLUE]<[/color] 0  den)
       )
       den
   )
)
([color=BLUE]defun[/color] setdensity ( key [color=BLUE]/[/color] def tmp )
   ([color=BLUE]initget[/color] 6)
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] def (getdensity key)
             tmp ([color=BLUE]cond[/color] (([color=BLUE]getreal[/color] ([color=BLUE]strcat[/color] [color=MAROON]"\nSpecify density (kg/m³)"[/color] ([color=BLUE]if[/color] def ([color=BLUE]strcat[/color] [color=MAROON]" <"[/color] ([color=BLUE]rtos[/color] def 2) [color=MAROON]">: "[/color]) [color=MAROON]": "[/color])))) (def))
       )
       ([color=BLUE]setenv[/color] key ([color=BLUE]rtos[/color] tmp 2))
   )
   tmp
)
([color=BLUE]vl-load-com[/color]) ([color=BLUE]princ[/color])

 

Thanks Lee, :thumbsup:

Link to comment
Share on other sites

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    5

  • naserrishehri@yahoo.com

    3

  • dpenney

    3

  • xces

    3

Since the user is specifying the density value, simply enter a value in kg/m3 (i.e. 1000x larger) and change the t/m3 text accordingly - the calculation will be identical.

 

Try the following:

([color=BLUE]defun[/color] c:sp ( [color=BLUE]/[/color] den idx key obj sel vol )
   ([color=BLUE]setq[/color] key [color=MAROON]"LMac\\solprop-density"[/color])
   ([color=BLUE]if[/color] ([color=BLUE]or[/color] ([color=BLUE]setq[/color] den (getdensity key))
           ([color=BLUE]setq[/color] den (setdensity key))
       )
       ([color=BLUE]while[/color]
           ([color=BLUE]progn[/color]
               ([color=BLUE]setvar[/color] 'errno 0)
               ([color=BLUE]initget[/color] [color=MAROON]"Multiple Density Exit"[/color])
               ([color=BLUE]princ[/color] ([color=BLUE]strcat[/color] [color=MAROON]"\nCurrent density: "[/color] ([color=BLUE]rtos[/color] den 2) [color=MAROON]" kg/m³"[/color]))
               ([color=BLUE]setq[/color] sel ([color=BLUE]entsel[/color] [color=MAROON]"\nSelect edge of solid object [Multiple/Density/Exit]: "[/color]))
               ([color=BLUE]cond[/color]
                   (   ([color=BLUE]=[/color] 7 ([color=BLUE]getvar[/color] 'errno))
                       ([color=BLUE]princ[/color] [color=MAROON]"\nMissed, try again."[/color])
                   )
                   (   ([color=BLUE]or[/color] ([color=BLUE]null[/color] sel) ([color=BLUE]=[/color] [color=MAROON]"Exit"[/color] sel))
                       [color=BLUE]nil[/color]
                   )
                   (   ([color=BLUE]=[/color] [color=MAROON]"Density"[/color] sel)
                       ([color=BLUE]setq[/color] den (setdensity key))
                   )
                   (   ([color=BLUE]=[/color] [color=MAROON]"Multiple"[/color] sel)
                       ([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] '((0 . [color=MAROON]"3DSOLID"[/color]))))
                           (showdensity den
                               ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] idx ([color=BLUE]sslength[/color] sel))
                                   ([color=BLUE]setq[/color] vol
                                       ([color=BLUE]+[/color]  ([color=BLUE]cond[/color] (vol) (0.0))
                                           ([color=BLUE]vla-get-volume[/color] ([color=BLUE]vlax-ename->vla-object[/color] ([color=BLUE]ssname[/color] sel ([color=BLUE]setq[/color] idx ([color=BLUE]1-[/color] idx)))))
                                       )
                                   )
                               )
                           )
                       )
                   )
                   (   ([color=BLUE]not[/color] ([color=BLUE]vlax-property-available-p[/color] ([color=BLUE]setq[/color] obj ([color=BLUE]vlax-ename->vla-object[/color] ([color=BLUE]car[/color] sel))) 'volume))
                       ([color=BLUE]princ[/color] [color=MAROON]"\nSelected object does not have Volume property."[/color])
                   )
                   (   (showdensity den ([color=BLUE]vla-get-volume[/color] obj)))
               )
           )
       )
   )
   ([color=BLUE]princ[/color])
)
([color=BLUE]defun[/color] showdensity ( den vol )
   ([color=BLUE]setq[/color] vol ([color=BLUE]/[/color] vol 1e9))
   ([color=BLUE]alert[/color]
       ([color=BLUE]strcat[/color]
           [color=MAROON]"Properties for a solid object at "[/color]  ([color=BLUE]rtos[/color] den 2 2) [color=MAROON]" kg/m³ : \n\n"[/color]
           [color=MAROON]"Volume = "[/color] ([color=BLUE]rtos[/color] vol 2 2) [color=MAROON]" m³\t\t"[/color]
           [color=MAROON]"Weight = "[/color] ([color=BLUE]rtos[/color] ([color=BLUE]*[/color] den vol) 2 2) [color=MAROON]" kg"[/color]
       )
   )
)
([color=BLUE]defun[/color] getdensity ( key [color=BLUE]/[/color] den )
   ([color=BLUE]if[/color] ([color=BLUE]and[/color] ([color=BLUE]setq[/color] den ([color=BLUE]getenv[/color] key))
            ([color=BLUE]setq[/color] den ([color=BLUE]distof[/color] den 2))
            ([color=BLUE]<[/color] 0  den)
       )
       den
   )
)
([color=BLUE]defun[/color] setdensity ( key [color=BLUE]/[/color] def tmp )
   ([color=BLUE]initget[/color] 6)
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] def (getdensity key)
             tmp ([color=BLUE]cond[/color] (([color=BLUE]getreal[/color] ([color=BLUE]strcat[/color] [color=MAROON]"\nSpecify density (kg/m³)"[/color] ([color=BLUE]if[/color] def ([color=BLUE]strcat[/color] [color=MAROON]" <"[/color] ([color=BLUE]rtos[/color] def 2) [color=MAROON]">: "[/color]) [color=MAROON]": "[/color])))) (def))
       )
       ([color=BLUE]setenv[/color] key ([color=BLUE]rtos[/color] tmp 2))
   )
   tmp
)
([color=BLUE]vl-load-com[/color]) ([color=BLUE]princ[/color])

 

Perfect. Many thanks for your time.

Link to comment
Share on other sites

  • 1 year later...

This is great! I've been looking for a lisp to help me generate a table summing the weight of solids sorted by materials/layers using the volume of the items and the layer's description to specify the lb/Lft. Do you think this routine could be modified to do this?

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