4 Replies Latest reply on May 23, 2016 7:28 AM by William Cruz

    Insert a plane at the highest face of a bounding box - C#

    William Cruz

      Is it possible to insert a plane at the highest face of a bounding box?

      I'm trying to create a custom SectionView and need to start from the top down.

        • Re: Insert a plane at the highest face of a bounding box - C#
          Christian Kniat

          If you add some pictures or a file we may be of more help.

            • Re: Insert a plane at the highest face of a bounding box - C#
              William Cruz

              When creating a SectionView, you need to select a plane or a face, it would be easier for me to just create a plane at the highest point of the assembly, to get the highest point I can just do a boundingbox and get the highest point that way. The code below shows how to create the SectionView based on the default TopPlane but I need to create a custom plane and select that one.


              swSectionViewData = ((SectionViewData)(_modDoc.ModelViewManager.CreateSectionViewData()));

                          Feature TopPlane = default(Feature);


                          _swModelDocExt.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);

                          TopPlane = (Feature)_swSelMgr.GetSelectedObject6(0, -1);

                          _swSectionViewData.FirstPlane = TopPlane;

                          _swSectionViewData.FirstReverseDirection = false;

                          _swSectionViewData.FirstOffset = TopMoveDist;//12.971526;

                          _swSectionViewData.FirstRotationX = 0;

                          _swSectionViewData.FirstRotationY = 0;

                          _swSectionViewData.FirstColor = 16711680;


                • Re: Insert a plane at the highest face of a bounding box - C#
                  Matthew Cempa

                  Here is my code for adding planes a the max and mins of a model parallel to a selected plane.  It's VBA but you can convert


                  Option Explicit

                  Sub main()
                      Dim swApp As SldWorks.SldWorks
                      Dim swModel As SldWorks.ModelDoc2
                      Dim swPart As SldWorks.PartDoc
                      Dim selMgr As SldWorks.SelectionMgr
                      Dim swMassProp As SldWorks.MassProperty
                      Dim swBody As SldWorks.Body2
                      Dim swMathUtil As SldWorks.MathUtility
                      Dim swFeature As SldWorks.Feature
                      Dim swRefPlane As SldWorks.RefPlane
                      Dim swRefPlaneID As Integer
                      Dim swRefPlaneTransform As SldWorks.MathTransform
                      Dim PlaneNormal As SldWorks.MathVector
                      Dim swOriginPoint            As SldWorks.MathPoint
                      Dim swFeatureManager As SldWorks.FeatureManager
                      Dim swMaxRefPlane As SldWorks.RefPlane
                      Dim swMinRefPlane As SldWorks.RefPlane
                      Dim vBodies As Variant

                      Dim lStatus As Long
                      Dim iStatus As Integer
                      Dim bStatus As Boolean
                      Dim i As Integer
                      Dim aPointData(2)            As Double
                      Dim vPointData               As Variant
                      Dim swOriginPointOnRefPlane  As SldWorks.MathPoint
                      Dim swNormalVector           As SldWorks.MathVector
                      Dim swNormalVectorRefPlane   As SldWorks.MathVector
                      Dim aVectorData(2)           As Double
                      Dim vVectorData              As Variant
                      Dim dScaleFactor             As Double
                      Dim zLimits(1) As Double
                      Dim dist As Double
                      Dim tempDist(1) As Double
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Set swPart = swModel
                      Set swMathUtil = swApp.GetMathUtility
                      Set selMgr = swModel.SelectionManager
                      Set swFeatureManager = swModel.FeatureManager
                      If selMgr.GetSelectedObjectCount2(-1) < 1 Then End
                      Set swFeature = selMgr.GetSelectedObject6(1, -1)
                      Set swRefPlane = swFeature.GetSpecificFeature2
                      Set swRefPlaneTransform = swRefPlane.Transform
                      ' Create array data
                      aPointData(0) = 0#
                      aPointData(1) = 0#
                      aPointData(2) = 0#
                      ' Turn into a Variant
                      vPointData = aPointData
                      ' Create a math point
                      Set swOriginPoint = swMathUtil.CreatePoint(vPointData)
                      ' Transform the reference plane origin from its canonical
                      ' position to its actual position
                      Set swOriginPointOnRefPlane = swOriginPoint.MultiplyTransform(swRefPlaneTransform)
                      ' Get point data
                      vPointData = swOriginPointOnRefPlane.ArrayData

                      ' Create array data
                      aVectorData(0) = 0#
                      aVectorData(1) = 0#
                      aVectorData(2) = 1#
                      ' Turn into a Variant
                      vVectorData = aVectorData
                      ' Create a math vector
                      Set swNormalVector = swMathUtil.CreateVector(vVectorData)

                      ' Now transform the reference plane normal from its canonical
                      ' orientation to its actual orientation
                      Set swNormalVectorRefPlane = swNormalVector.MultiplyTransform(swRefPlaneTransform)
                      ' Get vector data
                      vVectorData = swNormalVectorRefPlane.ArrayData
                      ' Loop thru the bodies and find their extreme points along the plane normal
                      'vBodies = swPart.GetBodies2(SwConst.swBodyType_e.swSolidBody, True)
                      vBodies = swPart.GetBodies2(SwConst.swBodyType_e.swSheetBody, True)
                      If Not IsEmpty(vBodies) Then
                          Set swBody = vBodies(0)

                          zLimits(0) = GetExtremeDistanceAlongAxis(swBody, swOriginPointOnRefPlane, swNormalVectorRefPlane)
                          zLimits(1) = GetExtremeDistanceAlongAxis(swBody, swOriginPointOnRefPlane, swNormalVectorRefPlane.Scale(-1))
                          For i = 1 To UBound(vBodies)
                              Set swBody = vBodies(i)
                              tempDist(0) = GetExtremeDistanceAlongAxis(swBody, swOriginPointOnRefPlane, swNormalVectorRefPlane)
                              tempDist(1) = GetExtremeDistanceAlongAxis(swBody, swOriginPointOnRefPlane, swNormalVectorRefPlane.Scale(-1))
                              If tempDist(0) > zLimits(0) Then zLimits(0) = tempDist(0)
                              If tempDist(1) > zLimits(1) Then zLimits(1) = tempDist(1)
                      End If

                      swModel.ClearSelection2 (True)
                      swFeature.Select2 False, 0
                      Set swMaxRefPlane = swFeatureManager.InsertRefPlane(swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_Distance, zLimits(0), 0, 0, 0, 0)
                      swFeature.Select2 False, 0
                      Set swMinRefPlane = swFeatureManager.InsertRefPlane(swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_Distance + swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_OptionFlip, zLimits(1), 0, 0, 0, 0)
                  End Sub

                  Function GetExtremeDistanceAlongAxis(swBody As SldWorks.Body2, origin As SldWorks.MathPoint, axis As SldWorks.MathVector) As Double
                      Dim swApp As SldWorks.SldWorks
                      Dim swMathUtil As SldWorks.MathUtility
                      Dim bStatus As Boolean
                      Dim extCoord(2) As Double
                      Dim extPt As SldWorks.MathPoint
                      Dim extVector As SldWorks.MathVector
                      Set swApp = Application.SldWorks
                      Set swMathUtil = swApp.GetMathUtility
                      bStatus = swBody.GetExtremePoint(axis.ArrayData(0), axis.ArrayData(1), axis.ArrayData(2), extCoord(0), extCoord(1), extCoord(2))
                          If bStatus Then
                              Set extPt = swMathUtil.CreatePoint(extCoord)
                              Set extVector = extPt.Subtract(origin)
                              GetExtremeDistanceAlongAxis = extVector.Dot(axis)
                          End If

                  End Function