length calculation error

length calculation error

Anonymous
Not applicable
1,317 Views
4 Replies
Message 1 of 5

length calculation error

Anonymous
Not applicable

I am trying to get the width and height of a block within my drawing. The block looks like a table, being made up of columns and rows of TEXT objects divided by and bounded by LINE objects. At first I used the bounds of the block and that worked great as long as their was no UCS. I have to account for UCS drawings. Soooo I have written the following code to go through the block and fine the line segments, get the length of those lines. The code runs ok, but the length values of the lines does not correspond to what I get when I explode the block and list the lines. For eg. The code says the line has a length of 85, but when you list it it's 81.6? The other dimension lines the code has it at 158.6 but when I list it it's 152.2???

 

You will see in the code I tried to grab all the X's and Y's of the line objects and then sort them to get min and max and figure it out that way too, but get the same results and just getting the LENGTH property.

 

Can anyone help me explain why the difference?

 

Dim blkheight As Double = GetblksizeUcs(blkobjref, 1)
Dim blkwidth As Double = GetblksizeUcs(blkobjref, 2)

 

Public Shadows Function GetblksizeUcs(ByVal blkref As BlockReference, ByVal dimtype As Integer) As Double
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim sizevalue As Double = 0
Dim XcoordList As List(Of Double) = New List(Of Double)
Dim YcoordList As List(Of Double) = New List(Of Double)

Using tr As Transaction = doc.TransactionManager.StartTransaction
Try
Dim blk As BlockTableRecord = DirectCast(tr.GetObject(blkref.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
Dim nrOfEnts As Integer = 0
For Each blkEntId As ObjectId In blk
Dim blkEnt As Entity = DirectCast(tr.GetObject(blkEntId, OpenMode.ForRead), Entity)
Dim enttype As String = blkEntId.ObjectClass.DxfName
If enttype = "LINE" Then
Dim PEnt As Line = blkEnt.ObjectId.GetObject(OpenMode.ForRead)
Dim startpt As Point3d = PEnt.StartPoint
Dim endpt As Point3d = PEnt.EndPoint
XcoordList.Add(startpt.X)
XcoordList.Add(endpt.X)
YcoordList.Add(startpt.Y)
YcoordList.Add(endpt.Y)
Dim linelength As Double = PEnt.Length

End If
Next
XcoordList.Sort()
YcoordList.Sort()

Dim minX As Double = XcoordList(0)
Dim maxX As Double = XcoordList(XcoordList.Count - 1)
Dim minY As Double = YcoordList(0)
Dim maxY As Double = YcoordList(YcoordList.Count - 1)

Dim BlkHieght As Double = maxY - minY
Dim BlkWidth As Double = maxX - minX

If dimtype = 1 Then
sizevalue = BlkHieght
Else
sizevalue = BlkWidth
End If
Catch
MsgBox("block has no lines within it")
End Try
End Using

Return sizevalue
End Function

0 Likes
Accepted solutions (1)
1,318 Views
4 Replies
Replies (4)
Message 2 of 5

hgasty1001
Advisor
Advisor

Hi,

 

Are you sure that min/max variables are calculated over the same entity? Also UCS changes preserves distances, so you don't need a special function to calculate lengths.

 

Gaston Nunez

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks for the suggestion Gaston.  I went back into my code and changed the color of the line. So I am getting the length of the right line(s). However, still have the issue.

 

From the .NET code, both the length property and calculating the length based on the coordinates match, however, when I take the distance of the line in autocad the numbers don't match up.  I have attached the drawing with the block so you have a much better idea what it looks like.

 

It seems like a simple enough task, but I'm missing something...even with fresh eyes on a monday, I don't see what is going wrong.

 

Any further help would be greatly appreciated.

 

thanks

Coralie

0 Likes
Message 4 of 5

Balaji_Ram
Alumni
Alumni
Accepted solution

Hi CJ,

 

The block reference seems to have a scaling of 1.6 

Other than accounting for that, i do not see any difference in the line lengths between what is in the BTR and when the BlockReference is exploded.

 

For example, 

 

When Iterating BTR and listing the line lengths :
74.9999999988358
75
75
86.8246930427849
86.8246930446476
86.8246930446476
When exploding BlockReference and listing the line lenghs:
119.999999998033
120.00000000002
120.00000000002
138.919508869067
138.919508873212
138.919508870061

 

After block ref was inserted with scale 1.0

 

When Iterating BTR and listing the line lengths :
74.9999999988358
75
75
86.8246930427849
86.8246930446476
86.8246930446476
When exploding BlockReference and listing the line lenghs:
74.9999999988358
75
75
86.8246930427849
86.8246930446476
86.8246930446476

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 5

Anonymous
Not applicable

Balaji,

 

It was the block scale that I missed!!!   I totally forgot to take that into concideration. 

 

thanks so much for taking the time to help. 

 

got things working like a charm now.

 

cj

0 Likes