I would like to get the intersection point of a 3D line which passes through a plane using MicrostationVBA (Microstation Version : 08.05.01.25 Windows x86).
Attached image for reference. I want to obtain the point(3D) at which the line (green line element) crosses the plane (yellow shape element)
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Plane3dIntersectsRay3d method fails for a particular element. How to fix this issue?
Sample code is given below and the Sample DGN is also attached.
Option Explicit Public Sub testPlaneIntersectRay() Dim shp As ShapeElement Dim lin As LineElement Dim plane As Plane3d Dim rayA As Ray3d Dim interPt As Point3d Dim param As Double Set shp = ActiveModelReference.GetElementByID(DLongFromLong(349743)) Set lin = ActiveModelReference.GetElementByID(DLongFromLong(349761)) If shp.IsPlanar Then plane.Normal = shp.Normal plane.Origin = shp.Vertex(1) rayA.Origin = lin.StartPoint rayA.Direction = Point3dSubtract(lin.EndPoint, lin.StartPoint) If Plane3dIntersectsRay3d(interPt, param, plane, rayA) Then Debug.Print interPt.X & "," & interPt.Y & "," & interPt.Z Else MsgBox "Intersection fails." End If End If End Sub
Test_Plane3dIntersectsRay3d.dgn
test_RayplaneIntersec_2.dgn
Hi Jan,
In the attached dgn, there is a shape element. While checking its planarity manually, it also displayed as "Non co-planar shapes" in Element information dialog as you said. But while checking using "IsPlanar", I find the element as planar and Plane3dIntersectsRay3d works in this case. Is there any other alternative to confirm the planarity manually since "Non co-planar shapes" in Element information dialog is not helping to arrive at a conclusion?
That's both weird and interesting. I assume a math behind the area calculation and VBA planarity test is different, so this particular shape is "enough planar" for VBA, but not "enough planar" for area calculation.
Unknown said:Is there any other alternative to confirm the planarity...
I tried to find some method/function in both VBA and MDL API and it seems there is no other direct method. But it's possible to implement own one using area measurement MDL function, which as a side effect provides information about a shape planarity:
Declare Function mdlMeasure_areaProperties Lib "stdmdlbltin.dll" (ByRef perimeterP As Double, _ ByRef areaP As Double, ByRef normalP As Point3d, ByRef centroidP As Point3d, _ ByRef momentP As Point3d, ByRef iXYP As Double, ByRef iXZP As Double, _ ByRef iYZP As Double, ByRef principalMomentsP As Point3d, _ ByRef principalDirectionsP As Point3d, ByVal edP As Long, _ ByVal tolerance As Double) As Long Private Function IsShapePlanar(shape As ShapeElement) As Boolean Dim result As Long Dim perimeterP As Double, areaP As Double Dim normalP As Point3d, centroidP As Point3d, momentP As Point3d Dim iXYP As Double, iXZP As Double, iYZP As Double Dim principalMomentsP As Point3d, principalDirectionsP As Point3d result = mdlMeasure_areaProperties(perimeterP, areaP, normalP, centroidP, momentP, _ iXYP, iXZP, iYZP, principalMomentsP, principalDirectionsP, shape.MdlElementDescrP, 0) If (result = -754) Then IsShapePlanar = False Else IsShapePlanar = True End If End Function
I am not sure if it's correct solution, but it works fine with your "maybe planar shape" (ID 202151) as it tells it's not planar, while VBA tells it's planar.
With regards,
Jan
Hi Jan
I have attached a zip file with two dgn files 'PlanarCheck_Dgn1.dgn' and 'PlanarCheck_Dgn2.dgn'.
Both dgns contain same element (shape element). While testing the planarity property of the element using VBA in both dgns, we get different results between the two dgns. Is this may be the problem due to dgn file or something else? Kindly present your views on this.
The VBA Code block that I used to test planarity is given below.
PlanarityCheckDGNs.zip
Public Sub checkPlanarity() Dim elmEnum As ElementEnumerator Dim elm As Element Set elmEnum = ActiveModelReference.GetSelectedElements elmEnum.MoveNext Set elm = elmEnum.Current If elm.AsShapeElement.IsPlanar Then MsgBox "Planar", vbInformation Else MsgBox "Non-Planar", vbCritical End If End Sub
Hi,
Unknown said:Is this may be the problem due to dgn file or something else?
Frankly, I am not sure why you are asking again if I provided my guess earlier already. The proven fact is that MDL function for area properties (mdlMeasure_areaProperties) provides different results than VBA IsPlanar method. In such situtaion, why do you want to discuss if it's cause by dgn if two functions provide two different results?
In my opinion the reason is MDL function is more strict and exact than IsPlanar, so in some tolerance VBA tells it's "enough planar" but if MDL is used, the same element "is not enough planar". The interpretation of the situation is up to you, you can e.g. take MDL as correct one, so consequently the problem is in DGN file, because the shape is not planar. Or you can say there is the bug in VBA API, because it tells the element is planar which is not.
But there is closely related question what does it mean "to be planar"? To define the planarity is not big problem, also to implement own planarity test is not huge task, but to make precision decision (because calculation using decimal floating point is never precise) is not easy and is very implementation and situation dependent.
In my opinion, because your original question was how to calculate shape / line intersection, is to use MDL for the planarity test and if it fails, to planarize the shape yourself, regardless VBA tells it's planar already.