Author Topic: Intersectwith method gives wrong results  (Read 2058 times)

0 Members and 1 Guest are viewing this topic.

gromit

  • Guest
Intersectwith method gives wrong results
« on: October 19, 2008, 05:30:52 PM »
Hello again

I've an xref which is an exploded polyface mesh so it consists of triangular faces. I am writing code to calculate levels on a line using the triangles in the mesh. I've written one version which works but which was slow - for every point in the polyline it looked through every triangle in the mesh until it found which triangle the point was in then interpolated the level. So I thought I could speed it up by reducing the number of triangles to loop through by finding which ones cut the polyline and then I only have to loop through these. Problem is that my code doesn't work - it says every triangle in the mesh intersects with the line.

Code follows (this is just the bit to find to filter the triangles, not the interpolation bits):

Code: [Select]
Sub LevelFromTria2()

    Dim objPLine As AcadLWPolyline
    Dim objTria As Acad3DFace
    Dim varCoords As Variant  '
    Dim objBlock As AcadBlock
    Dim lngCount As Long
    Dim objTriaSides As AcadLWPolyline
    Dim varIntPoints As Variant
    Dim dblTriaSidesCoords(0 To 5) As Double
    Dim varPnt As Variant
   
   
    On Error Resume Next '* Do so that if layer exists no error
    ThisDrawing.Layers.Add ("Temp Triangles")
    On Error GoTo 0

    ThisDrawing.Utility.GetEntity objPLine, varPnt, "Select polyline: "

    Set objBlock = ThisDrawing.Blocks("draw tria exploded")
   
    '* Go through all the items in the xref
    For lngCount = 0 To objBlock.Count - 1
   
        '* See if the item is a 3d face
        If objBlock.Item(lngCount).ObjectName = "AcDbFace" Then
            '* If it is get the face element
            Set objTria = objBlock.Item(lngCount)
            '* See if the face intersects with the polyline
            varIntPoints = objTria.IntersectWith(objPLine, acExtendNone)
           
            '* If it does, draw the triangle - TEMP MEASURE TO SEE IF IT IS FINDING THE RIGHT TRIANGLES
            If VarType(varIntPoints) <> vbEmpty Then
                '* Get co-ords of the 3d face
                varCoords = objTria.Coordinates
                '* Set up 2d coords for the triangle edge
                dblTriaSidesCoords(0) = varCoords(0)
                dblTriaSidesCoords(1) = varCoords(1)
                dblTriaSidesCoords(2) = varCoords(3)
                dblTriaSidesCoords(3) = varCoords(4)
                dblTriaSidesCoords(4) = varCoords(6)
                dblTriaSidesCoords(5) = varCoords(7)
                '* Add a LW polyline to show the triangle which intersects with the polyline
                Set objTriaSides = ThisDrawing.ModelSpace.AddLightWeightPolyline(dblTriaSidesCoords)
                objTriaSides.Closed = True
                objTriaSides.Layer = "Temp Triangles"
                objTriaSides.color = acMagenta
                objTriaSides.Update
            End If
           
        End If
   
    Next lngCount

                       
End Sub

Attached is the drawing I am testing this on. The line I am trying to get levels on is the yellow line - so pick this when prompted.

I'm thinking I've got something wrong with the intersect method. Rather than using the face itself I've also tried making a LW polyline from it and that doesn't work either.

Any suggestions as to how to get the above to work would be much appreciated. (Or if you have another concept for speeding up the way it gets levels onto the line from the triangulation I'd be interested to hear that too!)

Thanks

Jon

gromit

  • Guest
Re: Intersectwith method gives wrong results
« Reply #1 on: October 20, 2008, 02:47:26 AM »
PS: In the example drawing the line sits along the edge of many of the triangles, which could happen a lot in the situation I'm catering for. I don't know whether this is throwing the test I'm doing, but the weird thing is why is it selecting ALL the triangles?

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Intersectwith method gives wrong results
« Reply #2 on: October 21, 2008, 01:17:33 AM »
If VarType(varIntPoints) <> vbEmpty     doesn't work as a count of -1 (no intersection) is not vbempty

gromit

  • Guest
Re: Intersectwith method gives wrong results
« Reply #3 on: October 21, 2008, 03:13:33 AM »
Thanks Bryco. I found similar answer on the Autodesk forums. I was just following the help file - which is wrong!