VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dimension a line and add text above it

11 REPLIES 11
Reply
Message 1 of 12
Pretty
330 Views, 11 Replies

Dimension a line and add text above it

Can someone please tell me why the following lines in my code are not working? I am simply trying to dimension a line and write text above it.

Sub DimensionLine(obj As AcadObject, distFromObj As Double, dimText As String)


ThisDrawing.SetVariable "DIMTAD", 1 'Not Working
ThisDrawing.SetVariable "DIMJUST", 0 'Not Working
ThisDrawing.SetVariable "DIMGAP", 0.08 'Not Working


If obj.ObjectName = "AcDbLine" Then

Dim dimObj As AcadDimAligned
Dim dimPnt1 As Variant
Dim dimPnt2 As Variant
Dim location(0 To 2) As Double

dimPnt1 = obj.StartPoint
dimPnt2 = obj.EndPoint



location(0) = dimPnt1(0) + distFromObj
location(1) = dimPnt1(1) + distFromObj



Set dimObj = ThisDrawing.ModelSpace.AddDimAligned(dimPnt1, dimPnt2, location)
dimObj.TextInsideAlign = False
dimObj.Color = 252
dimObj.TextColor = acYellow
dimObj.AltSuppressTrailingZeros = True 'Not Working

dimObj.Update

Dim mTextObj As AcadMText
Dim width As Double
Dim txtInsPnt(0 To 2) As Double

txtInsPnt(0) = (dimObj.ExtLine1Point(0) + dimObj.ExtLine2Point(0)) / 2
txtInsPnt(1) = (dimObj.ExtLine1Point(1) + dimObj.ExtLine2Point(1)) / 2
txtInsPnt(2) = 0


width = 1


' Creates the mtext Object

Set mTextObj = ThisDrawing.ModelSpace.AddMText(txtInsPnt, width, dimText)
mTextObj.AttachmentPoint = acAttachmentPointTopCenter 'Not Working

mTextObj.Color = acYellow

ZoomAll

Else

MsgBox " Wrong object passed to DimensionLine. Object passed is " & obj.ObjectName


End If

End Sub
11 REPLIES 11
Message 2 of 12
Anonymous
in reply to: Pretty

Hi, Pretty
Try to suppress all lines, i.e. Extension lines, dimension lines etc
and you will be get a pure dimension text without any

~'J'~
Message 3 of 12
Anonymous
in reply to: Pretty

Pretty, I mean this one:

Set dimObj = ThisDrawing.ModelSpace.AddDimAligned(dimPnt1, dimPnt2, location)
dimObj.ExtLine1Suppress = True
dimObj.ExtLine2Suppress = True
dimObj.DimLine1Suppress = True
dimObj.DimLine2Suppress = True
dimObj.Arrowhead1Type = acArrowNone
dimObj.Arrowhead2Type = acArrowNone
dimObj.TextColor = acYellow
dimObj.TextInsideAlign = False
dimObj.TextGap = 0.08
dimObj.AltSuppressTrailingZeros = True

dimObj.Update

Hth

~'J'~
Message 4 of 12
Pretty
in reply to: Pretty

Hi Fatty,

First let me thank you for your reply.

See I want the dimension lines and extension lines. But I want Mtext to be displayed above the dimension text and the direction of the text should be along the dimension line. Right now whatever I do , the direction of the Mtext is horizontal, even if the line drawn is vertical.

So I did
mTextObj.AttachmentPoint = acAttachmentPointBottomCenter
But still nothing happens.

Also even if I change the values of the System Variables, nothing just happens. Can you tell me whats going wrong?

Thanks,
Pretty
Message 5 of 12
Anonymous
in reply to: Pretty

wrote in message news:5398692@discussion.autodesk.com...
Hi Fatty,

First let me thank you for your reply.

See I want the dimension lines and extension lines. But I want Mtext to be
displayed above the dimension text and the direction of the text should be
along the dimension line. Right now whatever I do , the direction of the
Mtext is horizontal, even if the line drawn is vertical.

So I did
mTextObj.AttachmentPoint = acAttachmentPointBottomCenter
But still nothing happens.

Also even if I change the values of the System Variables, nothing just
happens. Can you tell me whats going wrong?

Thanks,
Pretty

are you setting mTextObj.Rotation?
Message 6 of 12
Anonymous
in reply to: Pretty

Pretty,
I think there is no need to add mtext
Use TextPrefix property for dimension object instead

Try this one:

Set dimObj = ThisDrawing.ModelSpace.AddDimAligned(dimPnt1, dimPnt2, location)
dimObj.TextColor = acYellow
dimObj.TextInsideAlign = False
dimObj.TextGap = 0.08
dimObj.AltSuppressTrailingZeros = True
dimObj.TextPrefix = "Text Line1\PText Line2\PText Line3\P" ' use \P to jump next line
dimObj.Update

Hth

~'J'~
Message 7 of 12
Pretty
in reply to: Pretty

Hi MP,


I do not know the inclination of the line that I will dimension. So if I rotate Mtext so that Mtext is aligned properly for horizontal line, it is aligned wrongly for vertical line.

Also see
mTextObj.AttachmentPoint = acAttachmentPointBottomCenter
should align in a one way, i.e Mtext box should be placed above the line.

If I run my code with
mTextObj.AttachmentPoint = acAttachmentPointTopCenter
I should see Mtext box placed differently

But that is not happening.
Same with DIMJUST, and DIMTAD setting, it to 1 or 0 is not making any difference to the drawing.

So certain lines of code just dont seem to work while others are working perfectly.

Can you tell me what the problem could be.

Is it that system variables have to declared somewhere else (in the This Drawing or something and not in sub)?

Thanks for your help,
Pretty
Message 8 of 12
Pretty
in reply to: Pretty

Hi Fatty,

This is a very different idea. I will try this and get back to you.
In the meanwhile looking at the code, can you tell me why my system variables are not working. I.e even if I change their values, nothing is reflected in the drawing.

Thanks,
Pretty
Message 9 of 12
Pretty
in reply to: Pretty

Hi MP:

Just doing mTextObj.Rotation = obj.angle solves the problem. So thanks for the solution. Now that my mtext is aigned with the dimension line, any suggestion on how to get it above the dimesion line?

Thanks,
Pretty
Message 10 of 12
Pretty
in reply to: Pretty

Now that my mtext is aigned with the dimension line, any suggestion on how to get it above the dimesion line?

Thanks again,
Pretty
Message 11 of 12
Anonymous
in reply to: Pretty

wrote in message news:5400651@discussion.autodesk.com...
Now that my mtext is aigned with the dimension line, any suggestion on how
to get it above the dimesion line?

Thanks again,
Pretty

For Mtext, the location is determined by the combination of InsertionPoint
and AttachmentPoint
Read the help files on how they affect each other
Are you aware of the sub menu on the "Help" | "Additional Resources" |
"Developer Help" ?
Alt + H + R + P

this is different area of help system than F1, and has all the information
you need for this kind of task.

Hth
Mark
Message 12 of 12
Pretty
in reply to: Pretty

Hi Mark,

No I was not aware of that. Thanks for letting me know as F1 help is not always helpful.

Pretty

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost