PDA

View Full Version : LISP routine that will add a space in dimension text


irchrismm
2006-11-03, 08:01 PM
I am trying to set up a lisp routine that will give me a spacing in the dimension text, but I have no idea where to begin. I will give the inputs and the data I need.
Fist I need to choose either Slab thickness = getvar TH, or # of spacing = S.
Next I need to choose two points for the dimension. getpoint a1, getpoint a2.
Then since slab thickness affects my spacing, TH*8 = maxspacing.
If maxspacing is > 60, then spacing=60 otherwise spacing=TH*8
Spaces = distance of a1 & a2 / spacing
Now here is the part I can't figure out.
I need the dimension from a1 to a2. User places the third point of the dimension.
Plus I need a text override for the dimension text to look like "spacing" @ "spaces"%%P.
As well as, if there are less than 4 spaces, I need the result to be "spacing" @\X"spaces"%%P.
So far I have entered in the various formulas, but cannot get them on the dimension line.

Another option would be to set it up so I just place a bunch of dimensions and mass override the text with each figuring out the spacing based upon slab thickness or spaces.

Any help would be appreciated. I am very much a beginner in Lisp as I have not done any since college, and that was for version 13 then.

Chris

kennet.sjoberg
2006-11-03, 08:20 PM
Hi, here is something to start with. . .

(defun c:PreDimTxt (/ a1 a2 pt VL-Obj MyString )
(vl-load-com)
(setq a1 (getpoint "\nSpecify first extension line origin:" ) )
(setq a2 (getpoint "\nSpecify second extension line origin:" ) )
(setq pt (getpoint "\nSpecify dimension line location" ) )
(setq MyString "4 * 8 = " ) ;; <-- Your calculated string
(command "_dimlinear" a1 a2 pt )
(setq VL-Obj (vlax-ename->vla-Object (entlast )) )
(vlax-put-property VL-Obj "TextPrefix" MyString )
(vlax-release-Object VL-Obj )
(princ)
)

: ) Happy Computing !

kennet

fixo
2006-11-03, 09:19 PM
Kennet
You will beat me...


(defun c:Pfd (/ a1 a2 de elist MyString pt)
(setq a1 (getpoint "\nSpecify first extension line origin:" ) )
(setq a2 (getpoint a1 "\nSpecify second extension line origin:" ) )
(setq pt (getpoint a2 "\nSpecify dimension line location" ) )
(setq MyString "4 * 8 = " ) ;; <-- Your calculated string
(command "_.dimlinear" a1 a2 pt)
(setq de (entlast)
elist (entget de)
)
(entmod (subst (cons 1 (strcat
MyString
(rtos
(cdr (assoc 42 elist))
2 ;metric
(getvar "DIMDEC"); decimals
)))
(assoc 1 elist) elist))
(entupd de)
(princ)
)


>'J'<

kennet.sjoberg
2006-11-03, 11:19 PM
Kennet
You will beat me...
No No, I am cheating.
Chris was asking for AutoLisp.

You win fixo, as always. . .

: ) Happy Computing !

kennet

fixo
2006-11-03, 11:32 PM
No No, I am cheating.
Chris was asking for AutoLisp.

You win fixo, as always. . .

: ) Happy Computing !

kennet
C'mon Kennet

Happy computing :)

>'J'<

irchrismm
2006-11-03, 11:55 PM
For some reason I can get this to work in my Acad 2000, but not in my 2007 version. don't know why. Maybe I will try VBA. Time to start cracking open some books. Thanks for the help though. I really appreciate it.

Chris

fixo
2006-11-04, 08:52 AM
For some reason I can get this to work in my Acad 2000, but not in my 2007 version. don't know why. Maybe I will try VBA. Time to start cracking open some books. Thanks for the help though. I really appreciate it.

Chris
I haven't have A2007, sorry
Ok, in this case you can try it with VBA
As Kennet said:


Option Explicit

Sub AddDimPrefix()

Dim p1, p2, p3, _
ang As Double, _
MyString As String, _
oDim As AcadDimRotated

On Error GoTo err_control

With ThisDrawing.Utility
p1 = .GetPoint(, "Specify first extension line origin:")
p2 = .GetPoint(p1, "Specify second extension line origin:")
p3 = .GetPoint(p2, "Specify dimension line location:")
ang = .AngleFromXAxis(p1, p2)
End With

MyString = InputBox("Enter prefix for dimension text" & vbCr & _
"or press Enter to set default", "Add Prefix", "4 * 8 = ")
Set oDim = ThisDrawing.ActiveLayout.Block.AddDimRotated(p1, p2, p3, ang)
oDim.TextPrefix = MyString
oDim.Update

err_control:
If Err Then MsgBox Err.Description

End Sub


>'J'<

irchrismm
2006-11-08, 05:38 PM
Thanks guys,
I have moved this topic over to the VBA forum. I really appreciate it. I learned that AutoLisp changed over to Visual Lisp. I don't know the difference yet, ot why it matters, but apparently it does. I am running through tutorials on VBA, but as of yet, I can't quite wrap my brain around it.

Chris

kennet.sjoberg
2006-11-09, 04:53 PM
Hi Chris !

I suppose you do not know how to use the code, I have tested in 2007 and it works, but not in the way you want, your calculation formula is not there, your formula is substituted with a variable named MyString containing the stringvalue "4 * 8 = " that is added to the dimension text.
You can try the code doing this :

Mark the code in the forum from (defun . . .to the very last parenthesis )
right click the mouse button and select copy
switch to AutoCAD and place the mouse cursor and click on the very last line in the command window Command: |
right click the mouse button and select paste then press Enter
Now you have my code pasted and loaded, and the command (function) activated C: PreDimTxt
Next, type on the command line
Command: PreDimTxt
...and try your new created command

Better is to save my code in a file like PreDimTxt.lsp, modify it to suit your calculation
and load that file automatically in your startup suit, then the PreDimTxt command is always there.

: ) Happy Computing !

kennet

BTW why not upload your formula ?

irchrismm
2006-11-09, 06:44 PM
Ok, here is the formula I have.

Enter Slab thickness:
Pick Dimention Points:
p1=1st point
p2=2nd point
p3 = placement
Max Spacing = Slab thickness*8
If Slab thickness > 60 then Max Spacing = 60
Spaces = Distance from p1 to p2 / Max Spacing rounded to the nearest 1.
Spacing = Distance from p1 to p2 / Spaces Rounded to the nearest 1/2
Dimention text needs to have the following formula. "Spaces @ Spacing%%P"

As far as 2007, how do I get the Lisp to run? The dimension part of the Lisp works in my 2000 version, but not my 2007.

kennet.sjoberg
2006-11-09, 08:26 PM
Ok, here is the formula I have. . . .

(defun c:PreDimTxt (/ SlabTh p1 p2 p3 MaxSP Spaces Spacing MyString VL-Obj )
(vl-load-com)
;; Enter Slab thickness:
(setq SlabTh (getreal "Enter Slab thickness: " ) )
;; Pick Dimention Points:
;; p1=1st point
(setq p1 (getpoint "Pick Dimention Point p1: " ) )
;; p2=2nd point
(setq p2 (getpoint "Pick Dimention Point p2: " ) )
;; p3 = placement
(setq p3 (getpoint "Dimention placement Point p3: " ) )
;; Max Spacing = Slab thickness*8
;; If Slab thickness > 60 then Max Spacing = 60
(if (> SlabTh 60 ) (setq MaxSP 60 ) (setq MaxSP (* SlabTh 8 ) ) )
;; Spaces = Distance from p1 to p2 / Max Spacing rounded to the nearest 1.
(setq Spaces (fix (/ (distance p1 p2 ) MaxSP )) )
;; Spacing = Distance from p1 to p2 / Spaces Rounded to the nearest 1/2
(if (= Spaces 0 )
(setq Spacing (/ (distance p1 p2 ) 2 ) )
(setq Spacing (/ (distance p1 p2 ) Spaces 2 ) )
)
;; Dimention text needs to have the following formula. "Spaces @ Spacing%%P"
(setq MyString (strcat (itoa Spaces ) " @ " (rtos Spacing 2 2 ) " %%P " ) )
(command "_dimlinear" p1 p2 p3 )
(setq VL-Obj (vlax-ename->vla-Object (entlast )) )
; (vlax-put-property VL-Obj "TextPrefix" MyString ) ; Prefix
(vlax-put-property VL-Obj "TextOverride" MyString ) ;; Override
(vlax-release-Object VL-Obj )
(princ)
)

I still do not know what the result you are asking for shoud looks like, you may explane with a picture, or play with the code yourself.

As far as 2007, how do I get the Lisp to run? <-- In the same way as in 2002
The dimension part of the Lisp works in my 2000 version, but not my 2007.<--- Show the code it may fail in 2007 but not in 2000

: ) Happy Computing !

kennet

irchrismm
2006-11-09, 09:00 PM
Ok, here is an example.

Enter slab thickness: 6.5
Pick Dimension point 1:
Pick Dimension point 2:
Place dimension line:
in this case lets say the dimension = 20'-0"
Max spacing would be 6.5 x 8 or 52
So the number of spaces would be 20'-0" / 52 (rounded up to the nearest whole number)
in this case Spaces = 5
My spacing would be 20'-0" / 5 = 4'-0"
So my final result should look like this - "5 SP @ 4'-0"%%P" the overall dimension text would need to be overridden.
What you have given me is fantastic though. I have been racking my brain for quite a while trying to just get the basic formula to work. I still have not figured out how to convert everything from decimal to feet and inches, but I do have a couple of VBA that have these functions for Excel. Somehow I would have to either incorporate those, or another means within Lisp. Thanks for taking the time to look at this for me. Right now I have to place the dimension, and go into Excel and put this information in the spreadsheet, and then copy and paste the result back into the dimension text. It is very time consuming. I am trying to cut out the middle man and do it all within AutoCAD.

irchrismm
2006-11-15, 10:47 PM
Here is the code that I have for converting decimals to feet and inches, and Feet and inches to decimals.


Public Function feet(LenString As String)
Dim FootSign As Integer
Dim InchSign As Integer
Dim SpaceSign As Integer
Dim FracSign As Integer
Dim InchString As String
Dim Word2 As String
' Copyright 1999, 2005 MrExcel.com
LenString = Application.WorksheetFunction.Trim(LenString)
'The find function returns an error when the target is not found
'Resume Next will prevent VBA from halting execution.
On Error Resume Next
FootSign = Application.WorksheetFunction.Find("'", LenString)
If IsEmpty(FootSign) Or FootSign = 0 Then
' There are no feet in this expression
feet = 0
FootSign = 0
Else
feet = Val(Left(LenString, FootSign - 1))
End If

' Handle the case where the foot sign is the last character
If Len(LenString) = FootSign Then Exit Function
' Isolate the inch portion of the string
InchString = Application.WorksheetFunction.Trim(Mid(LenString, FootSign + 1))
' Strip off the inch sign, if there is one
InchSign = Application.WorksheetFunction.Find("""", InchString)
If Not IsEmpty(InchSign) Or InchSign = 0 Then
InchString = Application.WorksheetFunction.Trim(Left(InchString, InchSign - 1))
End If

' Do we have two words left, or one?
SpaceSign = Application.WorksheetFunction.Find(" ", InchString)
If IsEmpty(SpaceSign) Or SpaceSign = 0 Then
' There is only one word here. Is it inches or a fraction?
FracSign = Application.WorksheetFunction.Find("/", InchString)
If IsEmpty(FracSign) Or FracSign = 0 Then
'This word is inches
feet = feet + Val(InchString) / 12
Else
' This word is fractional inches
feet = feet + (Val(Left(InchString, FracSign - 1)) / Val(Mid(InchString, FracSign + 1))) / 12
End If
Else
' There are two words here. First word is inches
feet = feet + Val(Left(InchString, SpaceSign - 1)) / 12
' Second word is fractional inches
Word2 = Mid(InchString, SpaceSign + 1)
FracSign = Application.WorksheetFunction.Find("/", Word2)
If IsEmpty(FracSign) Or FracSign = 0 Then
' Return an error
feet = "VALUE!"
Else
If FracSign = 0 Then
feet = "VALUE!"
Else
feet = feet + (Val(Left(Word2, FracSign - 1)) / Val(Mid(Word2, FracSign + 1))) / 12
End If
End If
End If
End Function

I am also attaching the Excelfile I am using. I want to be able to do these functions in either AutoLisp or VBA in one command. Any help would be very very very appreciated.

Chris

[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]

kennet.sjoberg
2006-11-17, 09:21 PM
OK, one more attempt

Hey is this stupid Imperials a gift from the Neandertals ?
it brings me more problems than the lisp.
1'' Inch = 25.4 mm
1' Foot = 12 * 25.4 = 304.8 mm
1' Foot = 12'' Inch
20' Foot = 20 * 12'' = 240'' Inch = 240 * 25.4 = 6096 mm
6.5'' * 8 = 52'' = 52 * 25.4 = 1320.8
240'' / 52'' = 4.615.. --> 5
6096 / 1320.8 = 4.615.. --> 5
240'' / 5 = 48'' Inch
48'' / 12'' = 4' Foot
6096 / 5 = 1219.2
1219.2 / 304.8 = 4

(defun c:PreDimTxt (/ SlabTh p1 p2 p3 MaxSP Spaces Spacing MyString VL-Obj )
(vl-load-com)
;; Ok, here is an example.
;; Enter slab thickness: 6.5
(setq SlabTh (getreal "Enter Imperial Slab thickness : " ) )
;; Pick Dimension point 1:
(setq p1 (getpoint "Pick Dimention Point p1: " ) )
;; Pick Dimension point 2:
(setq p2 (getpoint "Pick Dimention Point p2: " ) )
;;Place dimension line:
(setq p3 (getpoint "Dimention placement Point p3: " ) )
;; Max Spacing = Slab thickness*8
;; in this case lets say the dimension = 20'-0"
;; If Slab thickness > 60 then Max Spacing = 60
;; Max spacing would be 6.5 x 8 or 52
(if (> SlabTh 60 ) (setq MaxSP 60 ) (setq MaxSP (* SlabTh 8 ) ) )
;; So the number of spaces would be 20'-0" / 52 (rounded up to the nearest whole number)
;; Spaces = Distance from p1 to p2 / Max Spacing rounded to the nearest 1.
;; in this case Spaces = 5
(setq Spaces (fix (+ 0.5 (/ (distance p1 p2 ) MaxSP ))) )
;; My spacing would be 20'-0" / 5 = 4'-0"
(setq Spacing (/ (distance p1 p2 ) Spaces ) )

;; Dimention text needs to have the following formula. "Spaces @ Spacing%%P"
; (setq MyString (strcat (itoa Spaces ) " @ " (rtos Spacing 3 2 ) " %%P " ) )
(setq MyString (strcat (itoa Spaces ) " @ " (rtos Spacing 4 2 ) " %%P " ) )
(command "_dimaligned" p1 p2 p3 )
(setq VL-Obj (vlax-ename->vla-Object (entlast )) )
; (vlax-put-property VL-Obj "TextPrefix" MyString ) ; Prefix
(vlax-put-property VL-Obj "TextOverride" MyString ) ;; Override
(vlax-release-Object VL-Obj )
(princ)
)

I hope you can use the code.

: ) Happy Computing !

kennet

irchrismm
2006-11-21, 02:36 PM
Sorry, I did not mention that I was using the imperial system. So many people from around the globe here use metric. Anywho....this is exactly what I need. Thank you very much. This will save me a whole lot of time.

Chris

irchrismm
2006-11-21, 03:37 PM
Ok on the same subject, I have made a couple of modifications to incorporate a couple of different scenarios we would encounter. 2 things I still need to overcome. 1st I wanted to have a manual spacing. So I changed the code from above to bypass most of the formulas, but it doesn't work. The other variations still do, but not this one. Here is what I have.
(defun c:mmspace (/ Spaces p1 p2 p3 Spaces Spacing MyString VL-Obj )
(vl-load-com)
;; Number of spaces
(setq Spaces (getreal "Enter number of spaces: " ) )
;; Pick Dimension point 1:
(setq p1 (getpoint "Pick Dimention Point p1: " ) )
;; Pick Dimension point 2:
(setq p2 (getpoint "Pick Dimention Point p2: " ) )
;;Place dimension line:
(setq p3 (getpoint "Dimention placement Point p3: " ) )
;; Spacing = spaces / distance
(setq Spacing (/ (distance p1 p2 ) Spaces ) )

;;"Spaces @ Spacing%%P"
; (setq MyString (strcat (itoa Spaces ) " SP @ " (rtos Spacing 3 2 ) " %%P " ) )
(setq MyString (strcat (itoa Spaces ) " SP @ " (rtos Spacing 4 2 ) " %%P " ) )
(command "_dimlinear" p1 p2 p3 )
(setq VL-Obj (vlax-ename->vla-Object (entlast )) )
; (vlax-put-property VL-Obj "TextPrefix" MyString ) ; Prefix
(vlax-put-property VL-Obj "TextOverride" MyString ) ;; Override
(vlax-release-Object VL-Obj )
(princ)

The 2nd thing is how can I round to the nearest even number in lisp?

I very much appreciate you guys for taking some time out and helping me with this.
If you are ever in Texas I will buy you a steak. :)
Chris

[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]