Jump to content

Is there a ddedit CALCULATOR?


coombsie11

Recommended Posts

Hi,

 

I know this is a shot in the dark, but worth asking anyway.

 

Is there a way that I can select 2 pieces of text in a drawing and the numeric values typed within that text are used to do a calculation and the output is made available as new text.

 

Eg

 

I often need to subtract a floor level value eg 50.02 from a ceiling level value eg 52.97 ......to give a floor to ceiling HEIGHT of 2.95. The 2 level values would be deleted and the calculated value of 2.95 would be added in a specific layer.

 

The calculation would always be a subtraction, but the values variable, with no constants.

 

To do this at the moment I use ddedit command and copy and paste the values to and from the standard windows calculator program. Alot of these calculations you can do in your head, but we dont like to risk a typo, because the consequences could be huge.

 

A command like this would be a life saver!!

 

But is it possible?

Link to comment
Share on other sites

this will do it, if you're going to be doing subtraction all the time, you could modify it to find the lesser of the two value and change it to a negative.

 

;* A0 command - adding machine

(defun C:AT (/ p l n e as total sqft at)

(setq sqft 0

p (ssget))

(if p

(progn

(setq l 0 n (sslength p))

(while (

(if (= "TEXT" (cdr (assoc 0 (setq e (entget (ssname p l))))))

(progn

(setq total (txt2num (cdr (setq as (assoc 1 e)))))

(setq sqft (+ sqft total))

)

)

(setq l (1+ l))

)

)

)

 

(setq at (getpoint "\nPick Text Insertion Point")

sqft (inscma sqft))

(command "text" at "" sqft))

(princ)

(defun txt2num (txt / num) ;provided by Jeff Mishler

(if (setq num (vl-list->string

(vl-remove-if-not

'(lambda (x)

(or (

(

)

)

(vl-string->list txt)

)

)

)

(atof num)

)

)

 

(defun inscma (sqf / # #1 )

(if (= # nil)

(setq # 0))

(setq #

(cond

((getint (strcat "\nEnter precision : "))) (#)))

(setq #1 #)

(setq sqft (rtos sqft 2 #1) #1 1)

(while (and (/= (substr sqft #1 1) ".")(

(setq #1 (1+ #1))

)

(setq #1 (1- #1) p# #1)

(if (= (setq #1 (rem #1 3)) 0)(setq #1 3))

(while (

(setq sqft (strcat (substr sqft 1 #1) "," (substr sqft (1+ #1)))

#1 (+ 4 #1)

p# (1+ p#)

)

)sqft)

Link to comment
Share on other sites

Thanks Tader,

 

Dont really understand lisp myself. Just hoping that such a great command exists and you have brought me some hope.

 

I tried to run the AT.lsp and it comes up with this on the command line

Command:
Command:
AT
Select objects: 1 found

Select objects: 1 found, 2 total

Select objects:
; error: no function definition: TXT2NUM

 

can you help??

Link to comment
Share on other sites

stick this in there, I look at changing it to find the smaller of two values and changing it to a negative in a little while.

 

(defun txt2num (txt / num) ;provided by Jeff Mishler ;txt is a string

(if (setq num (vl-list->string

(vl-remove-if-not

'(lambda (x)

(or (

(

)

)

(vl-string->list txt)

)

)

)

(atof num)

)

)

Link to comment
Share on other sites

Sorry Tader,

 

getting the same message.

 

I have probably done something incorrect?

 

I pasted the txt2num into the AT.lsp... Is this correct? Does it need to be pasted somewhere specific? or pasted to replace something? Or does it run as a seperate entity?

 

I am not familiar with lisp, so finding it hard to run, but I can see that a command like this would be such a bonus!

 

Any suggestions.:luck:

Link to comment
Share on other sites

Not sure if this is what you want, but worth a try anyway.

 

Let me know if it helps in any way :)

 

(defun c:calc (/)
   (setq oldlay (getvar "clayer"))
   (while
       (and
           (/= (setq txtsel1 (car (entsel "\nSelect First Numerical Text: "))) nil)
           (/= (setq txtsel2 (car (entsel "\nSelect Second Numerical Text: "))) nil)
       ) ; end and
       (setq txtent1 (entget txtsel1))
       (setq txtent2 (entget txtsel2))
       (if
           (and
               (setq num1 (atof (cdr (assoc 1 txtent1))))
               (setq num2 (atof (cdr (assoc 1 txtent2))))
           ) ; end and
           (progn
               (setq num3 (max num1 num2))
               (setq num4 (min num1 num2))
               (setq ans (- num3 num4))
               (setq pt1 (getpoint "\nSelect Point for Result: "))
               (entmake    (list    '(0 . "TEXT")
                           '(8 . "TEXT")
                           (cons 10 pt1)
                           (cons 40 2.5)
                           (cons 1 (rtos ans 2 2))
                           '(50 . 0.0)
                           '(7 . "STANDARD")
                           '(71 . 0)
                           '(72 . 1)
                           '(73 . 2)
                           (cons 11 pt1)
                       ) ; end list
               ) ; end entmake
               (entdel txtsel1)
               (entdel txtsel2)
           ) ; end progn
           (alert "Selection Must Be Numerical.")
       ) ; end if
   ) ; end while
   (setvar "clayer" oldlay)
   (prompt "\nFunction Complete.")
   (princ)
) ; end program
           
   
   

Link to comment
Share on other sites

Thank you thank you Lee Mac,:D

 

this is exactly what I was after. even puts the result in the correct layer for me. Brilliant..... This will save me a great deal of time in the future.

 

Dont know lisp, but I managed to adjust the text height to suit and have adapted a 2nd version that wont delete the 2 pieces of text. Easy enough, eh?

 

The only thing that I couldnt work out is how to change the text width factor. All of our STANDARD text has a width of 0.8, as opposed to 1.

 

If 0.8 is correct within STYLE, it still comes in at 1.

 

Can you help?

Link to comment
Share on other sites

Excellent, glad you could make use of it.

 

I used 'entdel' to delete the two selected text items, so remove these lines if need be. :)

 

To create the text I used a DXF table with the 'entmake' function so that any previous text settings were ignored and CAD creates the text 'from scratch'.

 

the DXF table contains various elements, each assigned a DXF group code:

 

i.e.

 

 (1 . "hello")  --  DXF code '1' is the text content
(50 . x) -- DXF code '50' is the rotation angle (x)
(41 . x) -- DXF code '41' is the width factor (x)

and so on....

 

and so, to modify my code slightly to incorporate a width factor of 0.8 and remove the entdel....

 

(defun c:calc (/ oldlay txtsel1 txtsel2 txtent1 txtent2 num1 num2 num3 num4 ans pt1)
   (setq oldlay (getvar "clayer"))
   (while
       (and
           (/= (setq txtsel1 (car (entsel "\nSelect First Numerical Text: "))) nil)
           (/= (setq txtsel2 (car (entsel "\nSelect Second Numerical Text: "))) nil)
       ) ; end and
       (setq txtent1 (entget txtsel1))
       (setq txtent2 (entget txtsel2))
       (if
           (and
               (setq num1 (atof (cdr (assoc 1 txtent1))))
               (setq num2 (atof (cdr (assoc 1 txtent2))))
           ) ; end and
           (progn
               (setq num3 (max num1 num2))
               (setq num4 (min num1 num2))
               (setq ans (- num3 num4))
               (setq pt1 (getpoint "\nSelect Point for Result: "))
               (entmake    (list    '(0 . "TEXT"); <<----<< Entity Type
                           '(8 . "TEXT"); <<----<< Layer Name
                           (cons 10 pt1); <<----<< Insertion Point
                           (cons 40 2.5); <<----<< Text Height
                           (cons 1 (rtos ans 2 2)); <<----<< Text Content
                           (cons 41 0.; <<----<< Width Factor
                           '(50 . 0.0); <<----<< Rotation Angle
                           '(7 . "STANDARD"); <<----<< Text Style
                           '(71 . 0)
                           '(72 . 1)
                           '(73 . 2)
                           (cons 11 pt1)
                       ) ; end list
               ) ; end entmake
               (entdel txtsel1) ; <<----<< Delete this line if needed
               (entdel txtsel2) ; <<----<< Delete this line if needed
           ) ; end progn
           (alert "Selection Must Be Numerical.")
       ) ; end if
   ) ; end while
   (setvar "clayer" oldlay)
   (prompt "\nFunction Complete.")
   (princ)
) ; end program
           
   

Link to comment
Share on other sites

Thanks Lee Mac, its perfect.

 

I will be using this one alot and will mention your name to the guys at work when I show it to them.

 

All of these shortcuts really add up, dont they?

 

:D:D

Link to comment
Share on other sites

Just thought of another thing.

 

I am sure its pretty sure its straightforward?

 

How can I make the text justify left, rather than middle center?

 

Is it a cons value??

Link to comment
Share on other sites

Hi Coombsie,

 

Yes, the justification is yet another DXF group code value, the 'cons' syntax that I use is not part of the DXF format, it just joins the code to the designated value.

 

 (cons 'a 2) returns (A . 2) 

 

And so to change the justification:

 

(defun c:calc (/ oldlay txtsel1 txtsel2 txtent1 txtent2 num1 num2 num3 num4 ans pt1)
   (setq oldlay (getvar "clayer"))
   (while
       (and
           (/= (setq txtsel1 (car (entsel "\nSelect First Numerical Text: "))) nil)
           (/= (setq txtsel2 (car (entsel "\nSelect Second Numerical Text: "))) nil)
       ) ; end and
       (setq txtent1 (entget txtsel1))
       (setq txtent2 (entget txtsel2))
       (if
           (and
               (setq num1 (atof (cdr (assoc 1 txtent1))))
               (setq num2 (atof (cdr (assoc 1 txtent2))))
           ) ; end and
           (progn
               (setq num3 (max num1 num2))
               (setq num4 (min num1 num2))
               (setq ans (- num3 num4))
               (setq pt1 (getpoint "\nSelect Point for Result: "))
               (entmake    (list    '(0 . "TEXT"); <<----<< Entity Type
                           '(8 . "TEXT"); <<----<< Layer Name
                           (cons 10 pt1); <<----<< Insertion Point
                           (cons 40 2.5); <<----<< Text Height
                           (cons 1 (rtos ans 2 2)); <<----<< Text Content
                           (cons 41 0.; <<----<< Width Factor
                           '(50 . 0.0); <<----<< Rotation Angle
                           '(7 . "STANDARD"); <<----<< Text Style
                           '(71 . 0)
                           '(72 . 0); <<----<< Horizontal Justification
                           '(73 . 0); <<----<< Vertical Justification
                           (cons 11 pt1)
                       ) ; end list
               ) ; end entmake
               (entdel txtsel1) ; <<----<< Delete this line if needed
               (entdel txtsel2) ; <<----<< Delete this line if needed
           ) ; end progn
           (alert "Selection Must Be Numerical.")
       ) ; end if
   ) ; end while
   (setvar "clayer" oldlay)
   (prompt "\nFunction Complete.")
   (princ)
) ; end program
           

 

 

To give you an idea of a DXF Table, type this in your AutoCAD command line:

 

(entget (car (entsel))) 

 

Then select an object and press 'F2' :)

 

 

Hope this helps! :D

Link to comment
Share on other sites

Looked at DXF Table. Very interesting.

 

It helps me to fill in some of the gaps and understand how some of the numbers in your lisp relate to the entities on screen.

 

I'm gonna sound picky now, but I was just using the calc.lsp and the text did not place correctly. I worked out that it was because I was not in World UCS.

Quite often I have to work in various UCS's.... just wondering if there was an easy fix? :)

Link to comment
Share on other sites

Hi Coombsie,

 

This is less than ideal, but it seems to work and so may be what you are after.

 

(defun c:calc (/ oldlay txtsel1 txtsel2 txtent1 txtent2 num1 num2 num3 num4 ans pt1)
   (setq oldlay (getvar "clayer"))
   (setvar "cmdecho" 0)
   (while
       (and
           (/= (setq txtsel1 (car (entsel "\nSelect First Numerical Text: "))) nil)
           (/= (setq txtsel2 (car (entsel "\nSelect Second Numerical Text: "))) nil)
       ) ; end and
       (setq txtent1 (entget txtsel1))
       (setq txtent2 (entget txtsel2))
       (if
           (and
               (setq num1 (atof (cdr (assoc 1 txtent1))))
               (setq num2 (atof (cdr (assoc 1 txtent2))))
           ) ; end and
           (progn
               (setq num3 (max num1 num2))
               (setq num4 (min num1 num2))
               (setq ans (- num3 num4))
               (command "UCS" "")
               (setq pt1 (getpoint "\nSelect Point for Result: "))
               (entmake    (list    '(0 . "TEXT"); <<----<< Entity Type
                           '(8 . "TEXT"); <<----<< Layer Name
                           (cons 10 pt1); <<----<< Insertion Point
                           (cons 40 2.5); <<----<< Text Height
                           (cons 1 (rtos ans 2 2)); <<----<< Text Content
                           (cons 41 0.; <<----<< Width Factor
                           '(50 . 0.0); <<----<< Rotation Angle
                           '(7 . "STANDARD"); <<----<< Text Style
                           '(71 . 0)
                           '(72 . 0); <<----<< Horizontal Justification
                           '(73 . 0); <<----<< Vertical Justification
                           (cons 11 pt1)
                       ) ; end list
               ) ; end entmake
               (command "UCS" "P")
               (entdel txtsel1) ; <<----<< Delete this line if needed
               (entdel txtsel2) ; <<----<< Delete this line if needed
           ) ; end progn
           (alert "Selection Must Be Numerical.")
       ) ; end if
   ) ; end while
   (setvar "clayer" oldlay)
   (setvar "cmdecho" 1)
   (prompt "\nFunction Complete.")
   (princ)
) ; end program
           
   

PS. Nothing is too picky... if it saves you time in the long run it is worth it :)

Link to comment
Share on other sites

Thanks once again Lee Mac.

 

Works like a dream.

 

I am really keen to get the hang of lisp and have ordered a book to help me along. Hopefully I will be able to give a little in the future, rather than take..

 

I used to be able play around with DIESEL within the LT versions of AutoCad, but it is far from ideal.

 

Thanks once again.:wink:

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