Imports System.Runtime.InteropServices

Public Class Form1
    Public objApp As SolidEdge.Framework.Interop.Application
    'Dim objDoc As Object
    Dim WithEvents objCmd As SolidEdge.Framework.Interop.Command
    Dim WithEvents objMouse As SolidEdge.Framework.Interop.Mouse
    Public CTR As Integer = 1

    Public objDoc As SolidEdge.Part.Interop.PartDocument = Nothing
    Public objAdjacentFace As SolidEdge.Geometry.Interop.Face = Nothing

    Public AxisVectorOfInputEdge(1) As Double
    Public dblNormalsFirstOpening(0 To 2) As Double
    Public dblNormalsSecondOpening(0 To 2) As Double
    Public dblNormalsThirdOpening(0 To 2) As Double
    Public dblNormalsFourthOpening(0 To 2) As Double
    Public objSelectedObject As Object = Nothing
    Public objModelBody As SolidEdge.Geometry.Interop.Body = Nothing
    Public objModelFaces As SolidEdge.Geometry.Interop.Faces = Nothing
    Public ObjEdge As SolidEdge.Geometry.Interop.Edge = Nothing
    Public Const dTol As Double = 0.00000001

    Public objCoordinateSystems As SolidEdge.Part.Interop.CoordinateSystems = Nothing
    Public objCoordinateSystem As SolidEdge.Part.Interop.CoordinateSystem = Nothing
    Public objRefXaxis As SolidEdge.Geometry.Interop.Edge = Nothing
    Public objCSParent As SolidEdge.Geometry.Interop.Edge = Nothing
    Public objCircle As SolidEdge.Geometry.Interop.Circle = Nothing
    Public objReference As SolidEdge.Framework.Interop.Reference = Nothing
    Public intNumberofConnectedFaces As Integer

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            ' ******need function to determine where SE is Loaded
            Call GetSEPath()
            Call GetLanguageFolder()

            ' **********  need function to pule the strings from the external drx file from the resdll/009 folder
            Dim ResHandle As Int32
            Dim ResFullPath As String
            Dim nSEVersion As Integer = Convert.ToInt32(strSEMajorVersion)

            ' language support
            If nSEVersion >= 110 And strLangId IsNot Nothing Then
                ResFullPath = strSEInstalledPath + "ResDLLs\" + strLangId + "\PreparePipingComponents.drx"
            Else
                ResFullPath = strSEInstalledPath + "ResDLLs\0009\PreparePipingComponents.drx"
            End If

            ResHandle = LoadResourceFile(ResFullPath)

            If 0 <> ResHandle Then
                SetResourceHandle(ResHandle)
                string1 = GetResourceString(1)
                string2 = GetResourceString(2)
                string3 = GetResourceString(3)
                string4 = GetResourceString(4)
                string5 = GetResourceString(5)
                string6 = GetResourceString(6)
                string7 = GetResourceString(7)
                string8 = GetResourceString(8)
                string9 = GetResourceString(9)
                string10 = GetResourceString(10)
                string11 = GetResourceString(11)
                string12 = GetResourceString(12)
                string13 = GetResourceString(13)
                string14 = GetResourceString(14)
                string15 = GetResourceString(15)
                string16 = GetResourceString(16)
                string17 = GetResourceString(17)
                string18 = GetResourceString(18)
                string19 = GetResourceString(19)
                string20 = GetResourceString(20)
                string21 = GetResourceString(21)
                string22 = GetResourceString(22)
                string23 = GetResourceString(23)
                string24 = GetResourceString(24)
                string25 = GetResourceString(25)
                string26 = GetResourceString(26)
                string27 = GetResourceString(27)
                string28 = GetResourceString(28)
                string29 = GetResourceString(29)
                string30 = GetResourceString(30)
                string31 = GetResourceString(31)
                string32 = GetResourceString(32)
                string33 = GetResourceString(33)
                string34 = GetResourceString(34)
                string35 = GetResourceString(35)
            Else
                'raise error
                MessageBox.Show("Could not extract the localized string from the file: " + ResFullPath, string8, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                End
            End If
            FreeLibrary(ResHandle)
        Catch ex As Exception
            MessageBox.Show(string1 + " " + ex.Message, string8, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            End
        End Try


        'load the pulled strings into the form

        Me.Text = string17
        Me.GroupBox1.Text = string18
        Me.GroupBox2.Text = string24
        Me.Button2.Text = string31
        Me.RB_Elbow.Text = string19
        Me.RBValve.Text = string20
        Me.RBTee.Text = string21
        Me.RBCross.Text = string22
        Me.RBPipe.Text = string23
        Me.CBxCommonDiameter2Angled.Text = string25
        Me.CBx2AngledDiffDiameter.Text = string26
        Me.Label2.Text = string27
        Me.Label3.Text = string28
        Me.Label4.Text = string29
        Me.Label5.Text = string30
        Me.ChkBxFlange.Text = string35

        '******* Added because of .NET
        Try
            MyMessageFilter.MessageFilter.Register()
        Catch ex As Exception
            'PrintLine("Error registering message filter.")
        End Try
        '******* Added because of .NET
        Try
            objApp = GetObject(, "SolidEdge.Application")
            objDoc = objApp.ActiveDocument
            Try
                If objDoc.Type = SolidEdge.Framework.Interop.DocumentTypeConstants.igPartDocument Then
                    If objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous Then
                        blnSynchronousMode = True
                        'make sure to switch to "ordered" mode to place the coordinate systems
                        objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeOrdered
                    End If
                End If


            Catch ex1 As Exception

            End Try

            RemovePipingRelatedCoordinateSystems(objDoc)
            RemovePipingRelatedVariables(objDoc)

            objCmd = objApp.CreateCommand(SolidEdge.Constant.Interop.seCmdFlag.seNoDeactivate)
            objMouse = objCmd.Mouse
            objCmd.Start()
            objApp.DoIdle()
        Catch ex As Exception
            MsgBox(string2 + " " + string1 + ":" + Err.Description.ToString)
            End
        End Try

        objMouse.DynamicsMode = SolidEdge.Constant.Interop.seDynamicsModes.seDynamicsOff
        objMouse.ClearLocateFilter()
        objMouse.AddToLocateFilter(SolidEdge.Constant.Interop.seLocateFilterConstants.seLocateEdge)
        objMouse.LocateMode = SolidEdge.Constant.Interop.seLocateModes.seLocateQuickPick

        Me.Label1.Text = string3

        objApp.StatusBar = string3



    End Sub

    Private Sub objMouse_MouseClick(ByVal sButton As Short, ByVal sShift As Short, ByVal dX As Double, ByVal dY As Double, ByVal dZ As Double, ByVal pWindowDispatch As Object, ByVal lKeyPointType As Integer, ByVal pGraphicDispatch As Object) Handles objMouse.MouseClick

        Dim ii As Integer
        Dim jj As Long = 0
        Dim ConnectedFaces(0) As Object
        Dim arrayMaxPoint(1) As Double
        Dim arrayMinPoint(1) As Double
        Dim dblPoints(2) As Double
        Dim dblGuessParams(3) As Double
        Dim dblMaxDev(1) As Double
        Dim dblParams(1) As Double
        Dim dblNormals(1) As Double
        Dim lngFlags(1) As Integer
        Dim CenterPoints(1) As Double
        Dim pp As Integer
        Dim intCompareThisAxis1 As Integer
        Dim intCompareThisAxis2 As Integer

        Dim objFaceToOrientYAxis As SolidEdge.Geometry.Interop.Face = Nothing
        Dim objSelectedFace As SolidEdge.Geometry.Interop.Face = Nothing

        Dim strExtrudedprotrusionVTName As String = ""
        Dim strLengthOfProtrusion As String = ""
        Dim zz As Integer




        intCompareThisAxis2 = 0
        intCompareThisAxis1 = 0


        If Me.TxtDiameter1.Text = "" Then
            MessageBox.Show(string34, string8, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
            Exit Sub
        End If


        If Me.RBDummy.Checked = True Then
            MessageBox.Show(string3, string8, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
            Exit Sub
        End If

        If pGraphicDispatch Is Nothing Then
            Exit Sub
        End If

        Try
            If objApp.ActiveDocumentType = SolidEdge.Framework.Interop.DocumentTypeConstants.igPartDocument Then
                objDoc = objApp.ActiveDocument
                objCoordinateSystems = objDoc.CoordinateSystems
            Else
                MessageBox.Show(string2, string8, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
                Exit Sub
            End If



            If Me.RBPipe.Checked = True Then   'pipes
                objSelectedObject = pGraphicDispatch


                'get the name of the parameter controlling the length of the protrusion
                Dim objFeatures As SolidEdge.Part.Interop.Features = Nothing
                Dim objProtrusion As SolidEdge.Part.Interop.ExtrudedProtrusion = Nothing

                Try
                    If objDoc.Models.Count > 0 Then
                        objFeatures = objDoc.Models.Item(1).Features
                        Dim intNumberOfDimensions As Integer
                        Dim objDims() As Object = {0}

                        For ii = 1 To objFeatures.Count

                            If objFeatures.Item(ii).type = SolidEdge.Part.Interop.FeatureTypeConstants.igExtrudedProtrusionFeatureObject Then
                                'grab the first extruded protrusion and get its length variable
                                objProtrusion = objFeatures.Item(ii)
                                objProtrusion.GetDimensions(intNumberOfDimensions, objDims)
                                If intNumberOfDimensions = 1 Then
                                    strExtrudedprotrusionVTName = objDims(0).systemname
                                    Garbage_Collect(objProtrusion)
                                    Garbage_Collect(objFeatures)
                                    Exit Try
                                End If
                            End If
                        Next

                        MessageBox.Show(string7, string8, MessageBoxButtons.OK, MessageBoxIcon.Error)

                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If

                        End

                    End If

                Catch ex As Exception

                End Try


                If objSelectedObject.type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igEdge Then
                    ObjEdge = pGraphicDispatch
                    Call ObjEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                    For ii = 1 To intNumberofConnectedFaces
                        objAdjacentFace = ConnectedFaces(ii - 1)
                        Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCylinder Or strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCone Or strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igTorus Then
                            If objFaceToOrientYAxis Is Nothing Then
                                objFaceToOrientYAxis = ConnectedFaces(ii - 1)
                            End If
                        End If
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                            objSelectedFace = ConnectedFaces(ii - 1)
                        End If
                    Next

                    'PR#6352258 added below
                    If objSelectedFace Is Nothing Then
                        For zz = 0 To ConnectedFaces.Count - 1
                            If ConnectedFaces(zz).Equals(objFaceToOrientYAxis) = False Then
                                Dim oFace As SolidEdge.Geometry.Interop.Face = ConnectedFaces(zz)
                                Dim oEdges As SolidEdge.Geometry.Interop.Edges = oFace.Edges
                                Dim oEdge As SolidEdge.Geometry.Interop.Edge = Nothing
                                For Each oEdge In oEdges
                                    If oEdge.Equals(ObjEdge) = False Then
                                        intNumberofConnectedFaces = 0
                                        Call oEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                                        For ii = 1 To intNumberofConnectedFaces
                                            objAdjacentFace = ConnectedFaces(ii - 1)
                                            Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                                            If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                                                objSelectedFace = ConnectedFaces(ii - 1)
                                                Garbage_Collect(oEdge)
                                                Garbage_Collect(oEdges)
                                                Garbage_Collect(oFace)
                                                Exit For
                                            End If
                                        Next
                                    End If
                                Next
                            End If
                        Next
                    End If
                    'PR#6352258 ###############


                    objCircle = ObjEdge.Geometry
                    If objSelectedFace Is Nothing Then   '  one of the attached faces to the input edge is not a plane.   need to find a plane to define the x axis
                        objCircle.GetAxisVector(AxisVector:=AxisVectorOfInputEdge)


                        'PR#6352258 commened out below
                        'If Math.Abs(AxisVectorOfInputEdge(0) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(0) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(1) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(1) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(2) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(2) = 0
                        'End If
                        'PR#6352258 ###############

                        ' PR#6352258 ###############
                        ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                        ' Added condition to check face normal going opposite direction
                        If Math.Abs(AxisVectorOfInputEdge(0)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(0) = 0
                        Else
                            If AxisVectorOfInputEdge(0) > 0 Then
                                AxisVectorOfInputEdge(0) = 1.0
                            Else
                                AxisVectorOfInputEdge(0) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(1)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(1) = 0
                        Else
                            If AxisVectorOfInputEdge(1) > 0 Then
                                AxisVectorOfInputEdge(1) = 1.0
                            Else
                                AxisVectorOfInputEdge(1) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(2)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(2) = 0
                        Else
                            If Math.Abs(AxisVectorOfInputEdge(2)) - 1.0 < dTol Then
                                If AxisVectorOfInputEdge(2) > 0 Then
                                    AxisVectorOfInputEdge(2) = 1.0
                                Else
                                    AxisVectorOfInputEdge(2) = -1.0
                                End If
                            End If
                        End If
                        ' PR#6352258 ###############
                        ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                        ' Added condition to check face normal going opposite direction

                        ' search body for face with normal going the same direction
                        If objDoc.Models.Count > 0 Then
                            objModelBody = objDoc.Models.Item(1).Body
                            objModelFaces = objModelBody.Faces(FaceType:=SolidEdge.Geometry.Interop.FeatureTopologyQueryTypeConstants.igQueryPlane)
                        End If
                        For jj = 1 To objModelFaces.Count
                            objSelectedFace = objModelFaces.Item(jj)
                            objSelectedFace.GetNormal(1, dblParams, dblNormals)
                            If Math.Abs(dblNormals(0) - 0.0) < dTol Then
                                dblNormals(0) = 0
                            End If
                            If Math.Abs(dblNormals(1) - 0.0) < dTol Then
                                dblNormals(1) = 0
                            End If
                            If Math.Abs(dblNormals(2) - 0.0) < dTol Then
                                dblNormals(2) = 0
                            End If
                            '' ''If -1 * dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                            '' ''    Exit For
                            '' ''End If
                            If (-1 * dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Or
                                (dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Or
                                (dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Then
                                If dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Or
                               (dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(0) = AxisVectorOfInputEdge(0) Then
                                    Exit For
                                End If
                            End If
                        Next
                    End If
                    objCircle.GetCenterPoint(dblPoints)   ' gets center point of input face

                    '##### put in code to double check center point using range API


                    '##### put in code to double check center point using range API
                    objCSParent = ObjEdge
                End If


                If objCircle Is Nothing Then
                    MessageBox.Show(string9, string8, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)

                    Exit Sub
                End If


                If objSelectedFace.Type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igFace Then
                    objSelectedFace.GetParamAtPoint(1, dblPoints, dblGuessParams, dblMaxDev, dblParams, lngFlags)
                    objSelectedFace.GetPointAtParam(1, dblParams, CenterPoints)
                    objSelectedFace.GetNormal(1, dblParams, dblNormals)
                End If


                ' 2 inline openings  is in the XZ plane   X (-) and X(+)

                'X(+)
                If Math.Round(dblNormals(0)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString

                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)

                        Dim objVariables As SolidEdge.Framework.Interop.Variables = Nothing
                        Dim objVariableList As SolidEdge.Framework.Interop.VariableList = Nothing
                        objVariables = objDoc.Variables
                        If strExtrudedprotrusionVTName <> "" Then
                            objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                            If objVariableList.Count = 1 Then
                                strLengthOfProtrusion = objVariableList.Item(1).value
                            Else
                                MessageBox.Show(string7, string8, MessageBoxButtons.OK, MessageBoxIcon.Error)
                                If blnSynchronousMode = True Then
                                    objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                                End If
                                End
                            End If
                        End If
                        AddToVariabletable("LL", objDoc.UnitsOfMeasure.FormatUnit(SolidEdge.Constant.Interop.UnitTypeConstants.igUnitDistance, strLengthOfProtrusion))
                        ' PR#8251585 - PreparePipingComponents.exe does not work since ST7..Sarang Kulkarni..11/03/14
                        ' This was regression caused due to fix added in HVariables::IHVariablesImp::Query() (Changeset 313996)
                        ' this fix clears all the variable list hence after calling AddToVariabletable() function objVariableList
                        ' becomes empty and setting formula function throw exception. If this list is empty Query variable again 
                        ' always get protrusion variable before setting LL formula to it
                        objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                        objVariableList.Item(1).Formula = "LL"
                        Garbage_Collect(objVariableList)
                        Garbage_Collect(objVariables)

                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter2.Text)
                        End If
                    End If

                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' X(-)
                If Math.Round(dblNormals(0)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)

                        Dim objVariables As SolidEdge.Framework.Interop.Variables = Nothing
                        Dim objVariableList As SolidEdge.Framework.Interop.VariableList = Nothing
                        objVariables = objDoc.Variables
                        If strExtrudedprotrusionVTName <> "" Then
                            objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                            If objVariableList.Count = 1 Then
                                strLengthOfProtrusion = objVariableList.Item(1).value
                            Else
                                MessageBox.Show(string7, string8, MessageBoxButtons.OK, MessageBoxIcon.Error)
                                If blnSynchronousMode = True Then
                                    objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                                End If
                                End
                            End If
                        End If
                        AddToVariabletable("LL", objDoc.UnitsOfMeasure.FormatUnit(SolidEdge.Constant.Interop.UnitTypeConstants.igUnitDistance, strLengthOfProtrusion))
                        ' always get protrusion variable before setting LL formula to it
                        objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                        objVariableList.Item(1).Formula = "LL"
                        Garbage_Collect(objVariableList)
                        Garbage_Collect(objVariables)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' Y (+)
                If Math.Round(dblNormals(1)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)

                        Dim objVariables As SolidEdge.Framework.Interop.Variables = Nothing
                        Dim objVariableList As SolidEdge.Framework.Interop.VariableList = Nothing
                        objVariables = objDoc.Variables
                        If strExtrudedprotrusionVTName <> "" Then
                            objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                            If objVariableList.Count = 1 Then
                                strLengthOfProtrusion = objVariableList.Item(1).value
                            Else
                                MessageBox.Show(string7, string8, MessageBoxButtons.OK, MessageBoxIcon.Error)
                                If blnSynchronousMode = True Then
                                    objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                                End If
                                End
                            End If
                        End If
                        AddToVariabletable("LL", objDoc.UnitsOfMeasure.FormatUnit(SolidEdge.Constant.Interop.UnitTypeConstants.igUnitDistance, strLengthOfProtrusion))
                        ' always get protrusion variable before setting LL formula to it
                        objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                        objVariableList.Item(1).Formula = "LL"
                        Garbage_Collect(objVariableList)
                        Garbage_Collect(objVariables)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' Y (-)
                If Math.Round(dblNormals(1)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)

                        Dim objVariables As SolidEdge.Framework.Interop.Variables = Nothing
                        Dim objVariableList As SolidEdge.Framework.Interop.VariableList = Nothing
                        objVariables = objDoc.Variables
                        If strExtrudedprotrusionVTName <> "" Then
                            objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                            If objVariableList.Count = 1 Then
                                strLengthOfProtrusion = objVariableList.Item(1).value
                            Else
                                MessageBox.Show(string7, string8, MessageBoxButtons.OK, MessageBoxIcon.Error)
                                If blnSynchronousMode = True Then
                                    objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                                End If
                                End
                            End If
                        End If
                        AddToVariabletable("LL", objDoc.UnitsOfMeasure.FormatUnit(SolidEdge.Constant.Interop.UnitTypeConstants.igUnitDistance, strLengthOfProtrusion))
                        ' always get protrusion variable before setting LL formula to it
                        objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                        objVariableList.Item(1).Formula = "LL"
                        Garbage_Collect(objVariableList)
                        Garbage_Collect(objVariables)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                'Z(+)
                If Math.Round(dblNormals(2)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)

                        Dim objVariables As SolidEdge.Framework.Interop.Variables = Nothing
                        Dim objVariableList As SolidEdge.Framework.Interop.VariableList = Nothing
                        objVariables = objDoc.Variables
                        If strExtrudedprotrusionVTName <> "" Then
                            objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                            If objVariableList.Count = 1 Then
                                strLengthOfProtrusion = objVariableList.Item(1).value
                            Else
                                MessageBox.Show(string7, string8, MessageBoxButtons.OK, MessageBoxIcon.Error)
                                If blnSynchronousMode = True Then
                                    objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                                End If
                                End
                            End If
                        End If
                        AddToVariabletable("LL", objDoc.UnitsOfMeasure.FormatUnit(SolidEdge.Constant.Interop.UnitTypeConstants.igUnitDistance, strLengthOfProtrusion))
                        ' always get protrusion variable before setting LL formula to it
                        objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                        objVariableList.Item(1).Formula = "LL"
                        Garbage_Collect(objVariableList)
                        Garbage_Collect(objVariables)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                'Z(-)
                If Math.Round(dblNormals(2)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter", Me.TxtDiameter1.Text)

                        Dim objVariables As SolidEdge.Framework.Interop.Variables = Nothing
                        Dim objVariableList As SolidEdge.Framework.Interop.VariableList = Nothing
                        objVariables = objDoc.Variables
                        If strExtrudedprotrusionVTName <> "" Then
                            objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                            If objVariableList.Count = 1 Then
                                strLengthOfProtrusion = objVariableList.Item(1).value
                            Else
                                MessageBox.Show(string7, string8, MessageBoxButtons.OK, MessageBoxIcon.Error)
                                If blnSynchronousMode = True Then
                                    objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                                End If
                                End
                            End If
                        End If
                        AddToVariabletable("LL", objDoc.UnitsOfMeasure.FormatUnit(SolidEdge.Constant.Interop.UnitTypeConstants.igUnitDistance, strLengthOfProtrusion))
                        ' always get protrusion variable before setting LL formula to it
                        objVariableList = objVariables.Query(strExtrudedprotrusionVTName)
                        objVariableList.Item(1).Formula = "LL"
                        Garbage_Collect(objVariableList)
                        Garbage_Collect(objVariables)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If


            End If





            If Me.RBCross.Checked = True Then    'cross type fittings
                objSelectedObject = pGraphicDispatch
                If objSelectedObject.type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igEdge Then
                    ObjEdge = pGraphicDispatch
                    Call ObjEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                    For ii = 1 To intNumberofConnectedFaces
                        objAdjacentFace = ConnectedFaces(ii - 1)
                        Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCylinder Or strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCone Then
                            If objFaceToOrientYAxis Is Nothing Then
                                objFaceToOrientYAxis = ConnectedFaces(ii - 1)
                            End If
                        End If
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                            objSelectedFace = ConnectedFaces(ii - 1)

                        End If
                    Next




                    'PR#6352258 added below
                    If objSelectedFace Is Nothing Then
                        For zz = 0 To ConnectedFaces.Count - 1
                            If ConnectedFaces(zz).Equals(objFaceToOrientYAxis) = False Then
                                Dim oFace As SolidEdge.Geometry.Interop.Face = ConnectedFaces(zz)
                                Dim oEdges As SolidEdge.Geometry.Interop.Edges = oFace.Edges
                                Dim oEdge As SolidEdge.Geometry.Interop.Edge = Nothing
                                For Each oEdge In oEdges
                                    If oEdge.Equals(ObjEdge) = False Then
                                        intNumberofConnectedFaces = 0
                                        Call oEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                                        For ii = 1 To intNumberofConnectedFaces
                                            objAdjacentFace = ConnectedFaces(ii - 1)
                                            Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                                            If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                                                objSelectedFace = ConnectedFaces(ii - 1)
                                                Garbage_Collect(oEdge)
                                                Garbage_Collect(oEdges)
                                                Garbage_Collect(oFace)
                                                Exit For
                                            End If
                                        Next
                                    End If
                                Next
                            End If
                        Next
                    End If
                    'PR#6352258 ###############


                    objCircle = ObjEdge.Geometry
                    objCircle.GetAxisVector(AxisVector:=AxisVectorOfInputEdge)


                    'PR#6352258 commened out below
                    'If Math.Abs(AxisVectorOfInputEdge(0) - 0.0) < dTol Then
                    '    AxisVectorOfInputEdge(0) = 0
                    'End If
                    'If Math.Abs(AxisVectorOfInputEdge(1) - 0.0) < dTol Then
                    '    AxisVectorOfInputEdge(1) = 0
                    'End If
                    'If Math.Abs(AxisVectorOfInputEdge(2) - 0.0) < dTol Then
                    '    AxisVectorOfInputEdge(2) = 0
                    'End If
                    'PR#6352258 ###############


                    ' PR#6352258 ###############
                    ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                    ' Added condition to check face normal going opposite direction
                    If Math.Abs(AxisVectorOfInputEdge(0)) - 0.0 < dTol Then
                        AxisVectorOfInputEdge(0) = 0
                    Else
                        If AxisVectorOfInputEdge(0) > 0 Then
                            AxisVectorOfInputEdge(0) = 1.0
                        Else
                            AxisVectorOfInputEdge(0) = -1.0
                        End If
                    End If
                    If Math.Abs(AxisVectorOfInputEdge(1)) - 0.0 < dTol Then
                        AxisVectorOfInputEdge(1) = 0
                    Else
                        If AxisVectorOfInputEdge(1) > 0 Then
                            AxisVectorOfInputEdge(1) = 1.0
                        Else
                            AxisVectorOfInputEdge(1) = -1.0
                        End If
                    End If
                    If Math.Abs(AxisVectorOfInputEdge(2)) - 0.0 < dTol Then
                        AxisVectorOfInputEdge(2) = 0
                    Else
                        If Math.Abs(AxisVectorOfInputEdge(2)) - 1.0 < dTol Then
                            If AxisVectorOfInputEdge(2) > 0 Then
                                AxisVectorOfInputEdge(2) = 1.0
                            Else
                                AxisVectorOfInputEdge(2) = -1.0
                            End If
                        End If
                    End If
                    'PR#6352258 ###############

                    If CTR = 1 Then    'check to make sure first 2 edges selected are in-line
                        dblNormalsFirstOpening(0) = AxisVectorOfInputEdge(0)
                        dblNormalsFirstOpening(1) = AxisVectorOfInputEdge(1)
                        dblNormalsFirstOpening(2) = AxisVectorOfInputEdge(2)
                    End If


                    If CTR = 2 Then    'check to make sure first 2 edges selected are in-line
                        dblNormalsSecondOpening(0) = AxisVectorOfInputEdge(0)
                        dblNormalsSecondOpening(1) = AxisVectorOfInputEdge(1)
                        dblNormalsSecondOpening(2) = AxisVectorOfInputEdge(2)
                    End If

                    If CTR = 3 Then    'check to make sure first 2 edges selected are in-line
                        dblNormalsThirdOpening(0) = AxisVectorOfInputEdge(0)
                        dblNormalsThirdOpening(1) = AxisVectorOfInputEdge(1)
                        dblNormalsThirdOpening(2) = AxisVectorOfInputEdge(2)
                    End If

                    If CTR = 4 Then    'check to make sure first 2 edges selected are in-line
                        dblNormalsFourthOpening(0) = AxisVectorOfInputEdge(0)
                        dblNormalsFourthOpening(1) = AxisVectorOfInputEdge(1)
                        dblNormalsFourthOpening(2) = AxisVectorOfInputEdge(2)
                    End If


                    If CTR = 2 Then 'check to make sure first 2 edges selected are in-line
                        For pp = 0 To dblNormalsFirstOpening.Length - 1
                            If dblNormalsFirstOpening(pp) <> 0 Then
                                intCompareThisAxis1 = pp
                            End If
                        Next

                        For pp = 0 To dblNormalsSecondOpening.Length - 1
                            If dblNormalsSecondOpening(pp) <> 0 Then
                                intCompareThisAxis2 = pp
                            End If
                        Next


                        If intCompareThisAxis1 <> intCompareThisAxis2 Then
                            RemovePipingRelatedCoordinateSystems(objDoc)
                            MessageBox.Show(string10, string8, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
                            CTR = 1
                            Exit Sub

                        End If
                    End If

                    If CTR = 4 Then 'check to make sure first 2 edges selected are in-line

                        For pp = 0 To dblNormalsThirdOpening.Length - 1
                            If dblNormalsThirdOpening(pp) <> 0 Then
                                intCompareThisAxis1 = pp
                            End If
                        Next

                        For pp = 0 To dblNormalsFourthOpening.Length - 1
                            If dblNormalsFourthOpening(pp) <> 0 Then
                                intCompareThisAxis2 = pp
                            End If
                        Next


                        If intCompareThisAxis1 <> intCompareThisAxis2 Then
                            RemovePipingRelatedCoordinateSystems(objDoc)
                            MessageBox.Show(string11, string8, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
                            CTR = 1
                            Exit Sub

                        End If
                    End If


                    If objSelectedFace Is Nothing Then   '  one of the attached faces to the input edge is not a plane.   need to find a plane to define the x axis


                        'PR#6352258 commened out below
                        'If Math.Abs(AxisVectorOfInputEdge(0) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(0) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(1) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(1) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(2) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(2) = 0
                        'End If
                        'PR#6352258 ###############


                        'PR#6352258 added below
                        If Math.Abs(AxisVectorOfInputEdge(0)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(0) = 0
                        Else
                            If AxisVectorOfInputEdge(0) > 0 Then
                                AxisVectorOfInputEdge(0) = 1.0
                            Else
                                AxisVectorOfInputEdge(0) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(1)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(1) = 0
                        Else
                            If AxisVectorOfInputEdge(1) > 0 Then
                                AxisVectorOfInputEdge(1) = 1.0
                            Else
                                AxisVectorOfInputEdge(1) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(2)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(2) = 0
                        Else
                            If Math.Abs(AxisVectorOfInputEdge(2)) - 1.0 < dTol Then
                                If AxisVectorOfInputEdge(2) > 0 Then
                                    AxisVectorOfInputEdge(2) = 1.0
                                Else
                                    AxisVectorOfInputEdge(2) = -1.0
                                End If
                            End If
                        End If

                        ' PR#6352258 ###############
                        ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                        ' Added condition to check face normal going opposite direction

                        ' search body for face with normal going the same direction
                        If objDoc.Models.Count > 0 Then
                            objModelBody = objDoc.Models.Item(1).Body
                            objModelFaces = objModelBody.Faces(FaceType:=SolidEdge.Geometry.Interop.FeatureTopologyQueryTypeConstants.igQueryPlane)
                        End If
                        For jj = 1 To objModelFaces.Count
                            objSelectedFace = objModelFaces.Item(jj)
                            objSelectedFace.GetNormal(1, dblParams, dblNormals)
                            If Math.Abs(dblNormals(0) - 0.0) < dTol Then
                                dblNormals(0) = 0
                            End If
                            If Math.Abs(dblNormals(1) - 0.0) < dTol Then
                                dblNormals(1) = 0
                            End If
                            If Math.Abs(dblNormals(2) - 0.0) < dTol Then
                                dblNormals(2) = 0
                            End If
                            '' ''If -1 * dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                            '' ''    Exit For
                            '' ''End If
                            If (-1 * dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Or
                                (dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Or
                                (dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Then
                                If dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Or
                                (dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(0) = AxisVectorOfInputEdge(0) Then
                                    Exit For
                                End If
                            End If


                        Next
                    End If
                End If
                objCircle.GetCenterPoint(dblPoints)   ' gets center point of input face

                '##### put in code to double check center point using range API


                '##### put in code to double check center point using range API
                objCSParent = ObjEdge
                'End If


                If objCircle Is Nothing Then
                    MessageBox.Show(string9, string8, MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Exit Sub
                End If

                If objSelectedFace.Type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igFace Then
                    objSelectedFace.GetParamAtPoint(1, dblPoints, dblGuessParams, dblMaxDev, dblParams, lngFlags)
                    objSelectedFace.GetPointAtParam(1, dblParams, CenterPoints)
                    objSelectedFace.GetNormal(1, dblParams, dblNormals)
                End If



                'X(+)
                If Math.Round(dblNormals(0)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                        If CTR = 4 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_4", CDbl(Me.TxtDiameter4.Text))
                            AddToVariabletable("Diameter_4", Me.TxtDiameter4.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 5 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' X(-)
                If Math.Round(dblNormals(0)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                        If CTR = 4 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_4", CDbl(Me.TxtDiameter4.Text))
                            AddToVariabletable("Diameter_4", Me.TxtDiameter4.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 5 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' Y (+)
                If Math.Round(dblNormals(1)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                        If CTR = 4 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_4", CDbl(Me.TxtDiameter4.Text))
                            AddToVariabletable("Diameter_4", Me.TxtDiameter4.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 5 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' Y (-)
                If Math.Round(dblNormals(1)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                        If CTR = 4 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_4", CDbl(Me.TxtDiameter4.Text))
                            AddToVariabletable("Diameter_4", Me.TxtDiameter4.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 5 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                'Z(+)
                If Math.Round(dblNormals(2)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                        If CTR = 4 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_4", CDbl(Me.TxtDiameter4.Text))
                            AddToVariabletable("Diameter_4", Me.TxtDiameter4.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 5 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                'Z(-)
                If Math.Round(dblNormals(2)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                        If CTR = 4 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_4", CDbl(Me.TxtDiameter4.Text))
                            AddToVariabletable("Diameter_4", Me.TxtDiameter4.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 5 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If


            End If





            If Me.RBTee.Checked = True Then    'TEE type fittings
                objSelectedObject = pGraphicDispatch
                If objSelectedObject.type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igEdge Then
                    ObjEdge = pGraphicDispatch
                    Call ObjEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                    For ii = 1 To intNumberofConnectedFaces
                        objAdjacentFace = ConnectedFaces(ii - 1)
                        Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCylinder Or strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCone Then
                            If objFaceToOrientYAxis Is Nothing Then
                                objFaceToOrientYAxis = ConnectedFaces(ii - 1)
                            End If
                        End If
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                            objSelectedFace = ConnectedFaces(ii - 1)
                        End If
                    Next

                    'PR#6352258 added below
                    If objSelectedFace Is Nothing Then
                        For zz = 0 To ConnectedFaces.Count - 1
                            If ConnectedFaces(zz).Equals(objFaceToOrientYAxis) = False Then
                                Dim oFace As SolidEdge.Geometry.Interop.Face = ConnectedFaces(zz)
                                Dim oEdges As SolidEdge.Geometry.Interop.Edges = oFace.Edges
                                Dim oEdge As SolidEdge.Geometry.Interop.Edge = Nothing
                                For Each oEdge In oEdges
                                    If oEdge.Equals(ObjEdge) = False Then
                                        intNumberofConnectedFaces = 0
                                        Call oEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                                        For ii = 1 To intNumberofConnectedFaces
                                            objAdjacentFace = ConnectedFaces(ii - 1)
                                            Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                                            If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                                                objSelectedFace = ConnectedFaces(ii - 1)
                                                Garbage_Collect(oEdge)
                                                Garbage_Collect(oEdges)
                                                Garbage_Collect(oFace)
                                                Exit For
                                            End If
                                        Next
                                    End If
                                Next
                            End If
                        Next
                    End If
                    'PR#6352258 ###############





                    objCircle = ObjEdge.Geometry
                    objCircle.GetAxisVector(AxisVector:=AxisVectorOfInputEdge)

                    'PR#6352258 commened out below
                    'If Math.Abs(AxisVectorOfInputEdge(0) - 0.0) < dTol Then
                    '    AxisVectorOfInputEdge(0) = 0
                    'End If
                    'If Math.Abs(AxisVectorOfInputEdge(1) - 0.0) < dTol Then
                    '    AxisVectorOfInputEdge(1) = 0
                    'End If
                    'If Math.Abs(AxisVectorOfInputEdge(2) - 0.0) < dTol Then
                    '    AxisVectorOfInputEdge(2) = 0
                    'End If
                    'PR#6352258 ###############


                    ' PR#6352258 ###############
                    ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                    ' Added condition to check face normal going opposite direction

                    If Math.Abs(AxisVectorOfInputEdge(0)) - 0.0 < dTol Then
                        AxisVectorOfInputEdge(0) = 0
                    Else
                        If AxisVectorOfInputEdge(0) > 0 Then
                            AxisVectorOfInputEdge(0) = 1.0
                        Else
                            AxisVectorOfInputEdge(0) = -1.0
                        End If
                    End If
                    If Math.Abs(AxisVectorOfInputEdge(1)) - 0.0 < dTol Then
                        AxisVectorOfInputEdge(1) = 0
                    Else
                        If AxisVectorOfInputEdge(1) > 0 Then
                            AxisVectorOfInputEdge(1) = 1.0
                        Else
                            AxisVectorOfInputEdge(1) = -1.0
                        End If
                    End If
                    If Math.Abs(AxisVectorOfInputEdge(2)) - 0.0 < dTol Then
                        AxisVectorOfInputEdge(2) = 0
                    Else
                        If Math.Abs(AxisVectorOfInputEdge(2)) - 1.0 < dTol Then
                            If AxisVectorOfInputEdge(2) > 0 Then
                                AxisVectorOfInputEdge(2) = 1.0
                            Else
                                AxisVectorOfInputEdge(2) = -1.0
                            End If
                        End If
                    End If
                    'PR#6352258 ###############




                    If CTR = 1 Then    'check to make sure first 2 edges selected are in-line
                        dblNormalsFirstOpening(0) = AxisVectorOfInputEdge(0)
                        dblNormalsFirstOpening(1) = AxisVectorOfInputEdge(1)
                        dblNormalsFirstOpening(2) = AxisVectorOfInputEdge(2)
                    End If


                    If CTR = 2 Then    'check to make sure first 2 edges selected are in-line
                        dblNormalsSecondOpening(0) = AxisVectorOfInputEdge(0)
                        dblNormalsSecondOpening(1) = AxisVectorOfInputEdge(1)
                        dblNormalsSecondOpening(2) = AxisVectorOfInputEdge(2)
                    End If



                    If CTR = 2 Then 'check to make sure first 2 edges selected are in-line
                        ' If -1 * dblNormalsFirstOpening(0) <> AxisVectorOfInputEdge(0) Then    
                        For pp = 0 To dblNormalsFirstOpening.Length - 1
                            If dblNormalsFirstOpening(pp) <> 0 Then
                                intCompareThisAxis1 = pp
                            End If
                        Next

                        For pp = 0 To dblNormalsSecondOpening.Length - 1
                            If dblNormalsSecondOpening(pp) <> 0 Then
                                intCompareThisAxis2 = pp
                            End If
                        Next


                        If intCompareThisAxis1 <> intCompareThisAxis2 Then
                            RemovePipingRelatedCoordinateSystems(objDoc)
                            MessageBox.Show(string10, string8, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
                            CTR = 1
                            Exit Sub

                        End If


                    End If






                    If objSelectedFace Is Nothing Then   '  one of the attached faces to the input edge is not a plane.   need to find a plane to define the x axis
                        'PR#6352258 commened out below
                        'If Math.Abs(AxisVectorOfInputEdge(0) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(0) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(1) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(1) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(2) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(2) = 0
                        'End If
                        'PR#6352258 ###############


                        'PR#6352258 added below
                        If Math.Abs(AxisVectorOfInputEdge(0)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(0) = 0
                        Else
                            If AxisVectorOfInputEdge(0) > 0 Then
                                AxisVectorOfInputEdge(0) = 1.0
                            Else
                                AxisVectorOfInputEdge(0) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(1)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(1) = 0
                        Else
                            If AxisVectorOfInputEdge(1) > 0 Then
                                AxisVectorOfInputEdge(1) = 1.0
                            Else
                                AxisVectorOfInputEdge(1) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(2)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(2) = 0
                        Else
                            If Math.Abs(AxisVectorOfInputEdge(2)) - 1.0 < dTol Then
                                If AxisVectorOfInputEdge(2) > 0 Then
                                    AxisVectorOfInputEdge(2) = 1.0
                                Else
                                    AxisVectorOfInputEdge(2) = -1.0
                                End If
                            End If
                        End If

                        ' PR#6352258 ###############
                        ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                        ' Added condition to check face normal going opposite direction
                        ' search body for face with normal going the same direction
                        If objDoc.Models.Count > 0 Then
                            objModelBody = objDoc.Models.Item(1).Body
                            objModelFaces = objModelBody.Faces(FaceType:=SolidEdge.Geometry.Interop.FeatureTopologyQueryTypeConstants.igQueryPlane)
                        End If
                        For jj = 1 To objModelFaces.Count
                            objSelectedFace = objModelFaces.Item(jj)
                            objSelectedFace.GetNormal(1, dblParams, dblNormals)
                            If Math.Abs(dblNormals(0) - 0.0) < dTol Then
                                dblNormals(0) = 0
                            End If
                            If Math.Abs(dblNormals(1) - 0.0) < dTol Then
                                dblNormals(1) = 0
                            End If
                            If Math.Abs(dblNormals(2) - 0.0) < dTol Then
                                dblNormals(2) = 0
                            End If
                            '' ''If -1 * dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                            '' ''    Exit For
                            '' ''End If
                            If (-1 * dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Or
                                (dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Or
                                (dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Then
                                If dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Or
                                (dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(0) = AxisVectorOfInputEdge(0) Then
                                    Exit For
                                End If
                            End If


                        Next
                    End If
                End If
                objCircle.GetCenterPoint(dblPoints)   ' gets center point of input face

                '##### put in code to double check center point using range API


                '##### put in code to double check center point using range API
                objCSParent = ObjEdge
                'End If


                If objCircle Is Nothing Then
                    MessageBox.Show(string9, string8, MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Exit Sub
                End If

                If objSelectedFace.Type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igFace Then
                    objSelectedFace.GetParamAtPoint(1, dblPoints, dblGuessParams, dblMaxDev, dblParams, lngFlags)
                    objSelectedFace.GetPointAtParam(1, dblParams, CenterPoints)
                    objSelectedFace.GetNormal(1, dblParams, dblNormals)
                End If



                'X(+)
                If Math.Round(dblNormals(0)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                    End If

                    CTR = CTR + 1
                    If CTR = 4 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' X(-)
                If Math.Round(dblNormals(0)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 4 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' Y (+)
                If Math.Round(dblNormals(1)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 4 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' Y (-)
                If Math.Round(dblNormals(1)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 4 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                'Z(+)
                If Math.Round(dblNormals(2)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 4 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                'Z(-)
                If Math.Round(dblNormals(2)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If

                        If CTR = 3 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_3", CDbl(Me.TxtDiameter3.Text))
                            AddToVariabletable("Diameter_3", Me.TxtDiameter3.Text)
                        End If
                    End If
                    CTR = CTR + 1
                    If CTR = 4 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If







            End If



            If Me.RB_Elbow.Checked = True Then    '2 angled fitting openings
                objSelectedObject = pGraphicDispatch
                If objSelectedObject.type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igEdge Then
                    ObjEdge = pGraphicDispatch
                    Call ObjEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                    For ii = 1 To intNumberofConnectedFaces
                        objAdjacentFace = ConnectedFaces(ii - 1)
                        Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCylinder Or strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCone Or strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igTorus Then
                            If objFaceToOrientYAxis Is Nothing Then
                                objFaceToOrientYAxis = ConnectedFaces(ii - 1)
                            End If
                        End If
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                            objSelectedFace = ConnectedFaces(ii - 1)
                        End If
                    Next


                    'PR#6352258 added below
                    If objSelectedFace Is Nothing Then
                        For zz = 0 To ConnectedFaces.Count - 1
                            If ConnectedFaces(zz).Equals(objFaceToOrientYAxis) = False Then
                                Dim oFace As SolidEdge.Geometry.Interop.Face = ConnectedFaces(zz)
                                Dim oEdges As SolidEdge.Geometry.Interop.Edges = oFace.Edges
                                Dim oEdge As SolidEdge.Geometry.Interop.Edge = Nothing
                                For Each oEdge In oEdges
                                    If oEdge.Equals(ObjEdge) = False Then
                                        intNumberofConnectedFaces = 0
                                        Call oEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                                        For ii = 1 To intNumberofConnectedFaces
                                            objAdjacentFace = ConnectedFaces(ii - 1)
                                            Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                                            If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                                                objSelectedFace = ConnectedFaces(ii - 1)
                                                Garbage_Collect(oEdge)
                                                Garbage_Collect(oEdges)
                                                Garbage_Collect(oFace)
                                                Exit For
                                            End If
                                        Next
                                    End If
                                Next
                            End If
                        Next
                    End If
                    'PR#6352258 ###############



                    objCircle = ObjEdge.Geometry
                    If objSelectedFace Is Nothing Then   '  one of the attached faces to the input edge is not a plane.   need to find a plane to define the x axis
                        objCircle.GetAxisVector(AxisVector:=AxisVectorOfInputEdge)
                        'PR#6352258 commened out below
                        'If Math.Abs(AxisVectorOfInputEdge(0) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(0) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(1) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(1) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(2) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(2) = 0
                        'End If
                        'PR#6352258 ###############

                        'PR#6352258 added below
                        If Math.Abs(AxisVectorOfInputEdge(0)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(0) = 0
                        Else
                            If AxisVectorOfInputEdge(0) > 0 Then
                                AxisVectorOfInputEdge(0) = 1.0
                            Else
                                AxisVectorOfInputEdge(0) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(1)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(1) = 0
                        Else
                            If AxisVectorOfInputEdge(1) > 0 Then
                                AxisVectorOfInputEdge(1) = 1.0
                            Else
                                AxisVectorOfInputEdge(1) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(2)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(2) = 0
                        Else
                            If Math.Abs(AxisVectorOfInputEdge(2)) - 1.0 < dTol Then
                                If AxisVectorOfInputEdge(2) > 0 Then
                                    AxisVectorOfInputEdge(2) = 1.0
                                Else
                                    AxisVectorOfInputEdge(2) = -1.0
                                End If
                            End If
                        End If
                        ' PR#6352258 ###############
                        ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                        ' Added condition to check face normal going opposite direction

                        ' search body for face with normal going the same direction
                        If objDoc.Models.Count > 0 Then
                            objModelBody = objDoc.Models.Item(1).Body
                            objModelFaces = objModelBody.Faces(FaceType:=SolidEdge.Geometry.Interop.FeatureTopologyQueryTypeConstants.igQueryPlane)
                        End If

                        For jj = 1 To objModelFaces.Count
                            objSelectedFace = objModelFaces.Item(jj)
                            objSelectedFace.GetNormal(1, dblParams, dblNormals)
                            If Math.Abs(dblNormals(0) - 0.0) < dTol Then
                                dblNormals(0) = 0
                            End If
                            If Math.Abs(dblNormals(1) - 0.0) < dTol Then
                                dblNormals(1) = 0
                            End If
                            If Math.Abs(dblNormals(2) - 0.0) < dTol Then
                                dblNormals(2) = 0
                            End If
                            '' ''If -1 * dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                            '' ''    Exit For
                            '' ''End If
                            If (-1 * dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Or
                                (dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Or
                                (dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Then
                                If dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Or
                                (dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(0) = AxisVectorOfInputEdge(0) Then
                                    Exit For
                                End If
                            End If
                        Next
                    End If
                    objCircle.GetCenterPoint(dblPoints)   ' gets center point of input face

                    '##### put in code to double check center point using range API


                    '##### put in code to double check center point using range API
                    objCSParent = ObjEdge
                    'End If


                    If objCircle Is Nothing Then
                        MessageBox.Show(string9, string8, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
                        Exit Sub
                    End If

                    If objSelectedFace.Type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igFace Then
                        objSelectedFace.GetParamAtPoint(1, dblPoints, dblGuessParams, dblMaxDev, dblParams, lngFlags)
                        objSelectedFace.GetPointAtParam(1, dblParams, CenterPoints)
                        objSelectedFace.GetNormal(1, dblParams, dblNormals)
                    End If


                    'X(+)
                    If Math.Round(dblNormals(0)) > 0 Then

                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                        If Me.CBxCommonDiameter2Angled.Checked = True Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If

                        If Me.CBx2AngledDiffDiameter.Checked = True Then
                            If CTR = 1 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                                AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                            End If
                            If CTR = 2 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                                AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                            End If
                        End If

                        CTR = CTR + 1
                        If CTR = 3 Then
                            CTR = 1
                            CleanUp()
                            objApp.StatusBar = ""
                            If blnSynchronousMode = True Then
                                objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                            End If
                            End
                        End If
                        Garbage_Collect(objSelectedFace)
                        Garbage_Collect(objFaceToOrientYAxis)
                        Exit Sub
                    End If

                    ' X(-)
                    If Math.Round(dblNormals(0)) < 0 Then
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                        If Me.CBxCommonDiameter2Angled.Checked = True Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If

                        If Me.CBx2AngledDiffDiameter.Checked = True Then
                            If CTR = 1 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                                AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                            End If
                            If CTR = 2 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                                AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                            End If
                        End If

                        CTR = CTR + 1
                        If CTR = 3 Then
                            CTR = 1
                            CleanUp()
                            objApp.StatusBar = ""
                            If blnSynchronousMode = True Then
                                objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                            End If
                            End
                        End If
                        Garbage_Collect(objSelectedFace)
                        Garbage_Collect(objFaceToOrientYAxis)
                        Exit Sub
                    End If

                    ' Y (+)
                    If Math.Round(dblNormals(1)) > 0 Then
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                        If Me.CBxCommonDiameter2Angled.Checked = True Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If

                        If Me.CBx2AngledDiffDiameter.Checked = True Then
                            If CTR = 1 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                                AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                            End If
                            If CTR = 2 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                                AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                            End If
                        End If

                        CTR = CTR + 1
                        If CTR = 3 Then
                            CTR = 1
                            CleanUp()
                            objApp.StatusBar = ""
                            If blnSynchronousMode = True Then
                                objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                            End If
                            End
                        End If
                        Garbage_Collect(objSelectedFace)
                        Garbage_Collect(objFaceToOrientYAxis)
                        Exit Sub
                    End If

                    ' Y (-)
                    If Math.Round(dblNormals(1)) < 0 Then
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                        If Me.CBxCommonDiameter2Angled.Checked = True Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If

                        If Me.CBx2AngledDiffDiameter.Checked = True Then
                            If CTR = 1 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                                AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                            End If
                            If CTR = 2 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                                AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                            End If
                        End If

                        CTR = CTR + 1
                        If CTR = 3 Then
                            CTR = 1
                            CleanUp()
                            objApp.StatusBar = ""
                            If blnSynchronousMode = True Then
                                objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                            End If
                            End
                        End If
                        Garbage_Collect(objSelectedFace)
                        Garbage_Collect(objFaceToOrientYAxis)
                        Exit Sub
                    End If

                    'Z(+)
                    If Math.Round(dblNormals(2)) > 0 Then
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                        If Me.CBxCommonDiameter2Angled.Checked = True Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If

                        If Me.CBx2AngledDiffDiameter.Checked = True Then
                            If CTR = 1 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                                AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                            End If
                            If CTR = 2 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                                AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                            End If
                        End If

                        CTR = CTR + 1
                        If CTR = 3 Then
                            CTR = 1
                            CleanUp()
                            objApp.StatusBar = ""
                            If blnSynchronousMode = True Then
                                objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                            End If
                            End
                        End If
                        Garbage_Collect(objSelectedFace)
                        Garbage_Collect(objFaceToOrientYAxis)
                        Exit Sub
                    End If

                    'Z(-)
                    If Math.Round(dblNormals(2)) < 0 Then

                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                        objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                        objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                        If Me.CBxCommonDiameter2Angled.Checked = True Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If

                        If Me.CBx2AngledDiffDiameter.Checked = True Then
                            If CTR = 1 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                                AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                            End If
                            If CTR = 2 Then
                                'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                                AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                            End If
                        End If

                        CTR = CTR + 1
                        If CTR = 3 Then
                            CTR = 1
                            CleanUp()
                            objApp.StatusBar = ""
                            If blnSynchronousMode = True Then
                                objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                            End If
                            End
                        End If
                        Garbage_Collect(objSelectedFace)
                        Garbage_Collect(objFaceToOrientYAxis)
                        Exit Sub
                    End If
                End If
            End If



            If Me.RBValve.Checked = True Then   '2 inline fitting openings
                objSelectedObject = pGraphicDispatch
                If objSelectedObject.type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igEdge Then
                    ObjEdge = pGraphicDispatch
                    Call ObjEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                    For ii = 1 To intNumberofConnectedFaces
                        objAdjacentFace = ConnectedFaces(ii - 1)
                        Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCylinder Or strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igCone Then
                            If objFaceToOrientYAxis Is Nothing Then
                                objFaceToOrientYAxis = ConnectedFaces(ii - 1)
                            End If
                        End If
                        If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                            objSelectedFace = ConnectedFaces(ii - 1)
                        End If
                    Next


                    'PR#6352258 added below
                    If objSelectedFace Is Nothing Then
                        For zz = 0 To ConnectedFaces.Count - 1
                            If ConnectedFaces(zz).Equals(objFaceToOrientYAxis) = False Then
                                Dim oFace As SolidEdge.Geometry.Interop.Face = ConnectedFaces(zz)
                                Dim oEdges As SolidEdge.Geometry.Interop.Edges = oFace.Edges
                                Dim oEdge As SolidEdge.Geometry.Interop.Edge = Nothing
                                For Each oEdge In oEdges
                                    If oEdge.Equals(ObjEdge) = False Then
                                        intNumberofConnectedFaces = 0
                                        Call oEdge.GetFaces(intNumberofConnectedFaces, ConnectedFaces)
                                        For ii = 1 To intNumberofConnectedFaces
                                            objAdjacentFace = ConnectedFaces(ii - 1)
                                            Dim strType As SolidEdge.Constant.Interop.GNTTypePropertyConstants = objAdjacentFace.Geometry.type
                                            If strType = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igPlane Then
                                                objSelectedFace = ConnectedFaces(ii - 1)
                                                Garbage_Collect(oEdge)
                                                Garbage_Collect(oEdges)
                                                Garbage_Collect(oFace)
                                                Exit For
                                            End If
                                        Next
                                    End If
                                Next
                            End If
                        Next
                    End If
                    ' PR#6352258 ###############
                    ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                    ' Added condition to check face normal going opposite direction

                    objCircle = ObjEdge.Geometry
                    If objSelectedFace Is Nothing Then   '  one of the attached faces to the input edge is not a plane.   need to find a plane to define the x axis
                        objCircle.GetAxisVector(AxisVector:=AxisVectorOfInputEdge)

                        'PR#6352258 commened out below
                        'If Math.Abs(AxisVectorOfInputEdge(0) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(0) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(1) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(1) = 0
                        'End If
                        'If Math.Abs(AxisVectorOfInputEdge(2) - 0.0) < dTol Then
                        '    AxisVectorOfInputEdge(2) = 0
                        'End If
                        'PR#6352258 ###############

                        'PR#6352258 added below
                        If Math.Abs(AxisVectorOfInputEdge(0)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(0) = 0
                        Else
                            If AxisVectorOfInputEdge(0) > 0 Then
                                AxisVectorOfInputEdge(0) = 1.0
                            Else
                                AxisVectorOfInputEdge(0) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(1)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(1) = 0
                        Else
                            If AxisVectorOfInputEdge(1) > 0 Then
                                AxisVectorOfInputEdge(1) = 1.0
                            Else
                                AxisVectorOfInputEdge(1) = -1.0
                            End If
                        End If
                        If Math.Abs(AxisVectorOfInputEdge(2)) - 0.0 < dTol Then
                            AxisVectorOfInputEdge(2) = 0
                        Else
                            If Math.Abs(AxisVectorOfInputEdge(2)) - 1.0 < dTol Then
                                If AxisVectorOfInputEdge(2) > 0 Then
                                    AxisVectorOfInputEdge(2) = 1.0
                                Else
                                    AxisVectorOfInputEdge(2) = -1.0
                                End If
                            End If
                        End If
                        'PR#6352258 ###############

                        ' search body for face with normal going the same direction
                        If objDoc.Models.Count > 0 Then
                            objModelBody = objDoc.Models.Item(1).Body
                            objModelFaces = objModelBody.Faces(FaceType:=SolidEdge.Geometry.Interop.FeatureTopologyQueryTypeConstants.igQueryPlane)
                        End If
                        For jj = 1 To objModelFaces.Count
                            objSelectedFace = objModelFaces.Item(jj)
                            objSelectedFace.GetNormal(1, dblParams, dblNormals)
                            'dblNormals(0) = CInt(dblNormals(0))
                            'dblNormals(1) = CInt(dblNormals(1))
                            'dblNormals(2) = CInt(dblNormals(2))


                            If Math.Abs(dblNormals(0) - 0.0) < dTol Then
                                dblNormals(0) = 0
                            End If
                            If Math.Abs(dblNormals(1) - 0.0) < dTol Then
                                dblNormals(1) = 0
                            End If
                            If Math.Abs(dblNormals(2) - 0.0) < dTol Then
                                dblNormals(2) = 0
                            End If

                            ' PR#6352258 ###############
                            ' PR#7505466 - PreparePipingComponents.exe in supplied piping Utility folder creates bad CS..SArang Kulkatni..11/5/15
                            ' Added condition to check face normal going opposite direction

                            '' ''If -1 * dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                            '' ''    Exit For
                            '' ''End If
                            If (-1 * dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Or
                                (dblNormals(0) = AxisVectorOfInputEdge(0) And AxisVectorOfInputEdge(0) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Or
                                (dblNormals(1) = AxisVectorOfInputEdge(1) And AxisVectorOfInputEdge(1) <> 0) Then
                                If dblNormals(0) = AxisVectorOfInputEdge(0) And dblNormals(2) = AxisVectorOfInputEdge(2) Then
                                    Exit For
                                End If
                            End If

                            If (-1 * dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Or
                                (dblNormals(2) = AxisVectorOfInputEdge(2) And AxisVectorOfInputEdge(2) <> 0) Then
                                If dblNormals(1) = AxisVectorOfInputEdge(1) And dblNormals(0) = AxisVectorOfInputEdge(0) Then
                                    Exit For
                                End If
                            End If
                        Next
                    End If
                    objCircle.GetCenterPoint(dblPoints)   ' gets center point of input face

                    '##### put in code to double check center point using range API


                    '##### put in code to double check center point using range API
                    objCSParent = ObjEdge
                End If


                If objCircle Is Nothing Then
                    MessageBox.Show(string9, string8, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)

                    Exit Sub
                End If


                If objSelectedFace.Type = SolidEdge.Constant.Interop.GNTTypePropertyConstants.igFace Then
                    objSelectedFace.GetParamAtPoint(1, dblPoints, dblGuessParams, dblMaxDev, dblParams, lngFlags)
                    objSelectedFace.GetPointAtParam(1, dblParams, CenterPoints)
                    objSelectedFace.GetNormal(1, dblParams, dblNormals)
                End If



                If Me.ChkBxFlange.Checked = True Then
                    AddToVariabletable("IsFlange", "1")
                Else
                    AddToVariabletable("IsFlange", "0")
                End If


                ' 2 inline openings  is in the XZ plane   X (-) and X(+)

                'X(+)
                If Math.Round(dblNormals(0)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString

                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If
                    End If

                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' X(-)
                If Math.Round(dblNormals(0)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' Y (+)
                If Math.Round(dblNormals(1)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                ' Y (-)
                If Math.Round(dblNormals(1)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                'Z(+)
                If Math.Round(dblNormals(2)) > 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""
                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If
                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If

                'Z(-)
                If Math.Round(dblNormals(2)) < 0 Then
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igRight, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "INPUT_POINT_" + CTR.ToString
                    objCoordinateSystem = objCoordinateSystems.AddByGeometry(SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysXAxis, objSelectedFace, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, SolidEdge.Part.Interop.CoordinateSystemFeatureConstants.seCoordSysYAxis, objFaceToOrientYAxis, SolidEdge.Part.Interop.FeaturePropertyConstants.igLeft, objCSParent, SolidEdge.Framework.Interop.KeyPointType.igKeyPointCenter, 0, 0, 0)
                    objCoordinateSystem.Name = "OUTPUT_POINT_" + CTR.ToString
                    If Me.CBxCommonDiameter2Angled.Checked = True Then
                        'objApp.ActiveDocument.properties.item("Custom").add("Diameter", CDbl(Me.TxtDiameter1.Text))
                        AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                    End If

                    If Me.CBx2AngledDiffDiameter.Checked = True Then
                        If CTR = 1 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_1", CDbl(Me.TxtDiameter1.Text))
                            AddToVariabletable("Diameter_1", Me.TxtDiameter1.Text)
                        End If
                        If CTR = 2 Then
                            'objApp.ActiveDocument.properties.item("Custom").add("Diameter_2", CDbl(Me.TxtDiameter2.Text))
                            AddToVariabletable("Diameter_2", Me.TxtDiameter2.Text)
                        End If
                    End If


                    CTR = CTR + 1
                    If CTR = 3 Then
                        CTR = 1
                        CleanUp()
                        objApp.StatusBar = ""

                        If blnSynchronousMode = True Then
                            objDoc.ModelingMode = SolidEdge.Part.Interop.ModelingModeConstants.seModelingModeSynchronous
                        End If

                        End
                    End If
                    Garbage_Collect(objSelectedFace)
                    Garbage_Collect(objFaceToOrientYAxis)
                    Exit Sub
                End If


            End If

        Catch ex As Exception

        End Try
    End Sub





    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub


    Public Sub CleanUp()

        '******* Added because of .NET
        Try
            MyMessageFilter.MessageFilter.Revoke()
        Catch ex As Exception
            ' PrintLine("Error revoking the message filter.")
        End Try
        '******* Added because of .NET

        objCmd.Done = True


        Garbage_Collect(objCoordinateSystem)
        Garbage_Collect(objCoordinateSystems)
        Garbage_Collect(objCSParent)
        Garbage_Collect(objCircle)
        'Garbage_Collect(objFaceToOrientYAxis)
        Garbage_Collect(objAdjacentFace)
        Garbage_Collect(ObjEdge)
        'Garbage_Collect(objSelectedFace)
        Garbage_Collect(objSelectedObject)
        Garbage_Collect(objModelBody)
        Garbage_Collect(objModelFaces)
        'Garbage_Collect(objSelectSet)
        Garbage_Collect(objDoc)

    End Sub

    Private Sub RBTee_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RBTee.CheckedChanged


        Me.CBxCommonDiameter2Angled.Checked = False
        Me.CBx2AngledDiffDiameter.Checked = False
        Me.TxtDiameter1.Enabled = False
        Me.TxtDiameter2.Enabled = False
        Me.TxtDiameter3.Enabled = False
        Me.TxtDiameter4.Enabled = False



        If Me.RBTee.Checked = True Then
            Me.Label1.Text = string12
            Me.Label1.Refresh()
            objApp.StatusBar = string12
        End If
    End Sub

    Private Sub RB_Elbow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RB_Elbow.CheckedChanged


        Me.CBxCommonDiameter2Angled.Checked = False
        Me.CBx2AngledDiffDiameter.Checked = False
        Me.TxtDiameter1.Enabled = False
        Me.TxtDiameter2.Enabled = False
        Me.TxtDiameter3.Enabled = False
        Me.TxtDiameter4.Enabled = False




        If Me.RB_Elbow.Checked = True Then
            Me.Label1.Text = string13
            Me.Label1.Refresh()
            objApp.StatusBar = string13
        End If

    End Sub

    Private Sub RBValve_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RBValve.CheckedChanged

        Me.CBxCommonDiameter2Angled.Checked = False
        Me.CBx2AngledDiffDiameter.Checked = False
        Me.TxtDiameter1.Enabled = False
        Me.TxtDiameter2.Enabled = False
        Me.TxtDiameter3.Enabled = False
        Me.TxtDiameter4.Enabled = False


        If Me.RBValve.Checked = True Then
            Me.Label1.Text = string14
            Me.Label1.Refresh()
            objApp.StatusBar = string14
            Me.ChkBxFlange.Enabled = True
        End If

        If Me.RBValve.Checked = False Then
            Me.ChkBxFlange.Enabled = False

        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        objApp.StatusBar = ""
        End
    End Sub


    Public Sub RemovePipingRelatedCoordinateSystems(ByVal oDoc As SolidEdge.Part.Interop.PartDocument)


        Try

            Dim objCS As SolidEdge.Part.Interop.CoordinateSystem = Nothing
            Dim objCSs As SolidEdge.Part.Interop.CoordinateSystems = Nothing
            Dim objSelectSet As SolidEdge.Framework.Interop.SelectSet = Nothing
            objSelectSet = oDoc.SelectSet
            objCSs = oDoc.CoordinateSystems

            objSelectSet.RemoveAll()

            For Each objCS In objCSs
                If InStr(objCS.EdgebarName, "INPUT_POINT", CompareMethod.Text) <> 0 Then
                    objSelectSet.Add(objCS)
                End If
                If InStr(objCS.EdgebarName, "OUTPUT_POINT", CompareMethod.Text) <> 0 Then
                    objSelectSet.Add(objCS)
                End If
            Next
            If objSelectSet.Count > 0 Then
                objSelectSet.Delete()
            End If



            If objCSs.Count > 1 Then
                'there is still coordinate systems in the file
                'ER 1900386  having extra cordinate system in the file that is not required for piping command causes a failure
                'as a workaround added the code below to delete ALL coordinatesystems except the base one.
                For Each objCS In objCSs
                    Dim strName As String = objCS.SystemName
                    If strName.ToUpper <> "COORDINATESYSTEM_1" Then
                        objSelectSet.Add(objCS)
                    End If
                Next
                If objSelectSet.Count > 0 Then
                    objSelectSet.Delete()
                End If
            End If

            Garbage_Collect(objSelectSet)
            Garbage_Collect(objCS)
            Garbage_Collect(objCSs)
            ' Garbage_Collect(oDoc)
        Catch ex As Exception
            'raise string4
            MessageBox.Show(string4, string8, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
            End
        End Try

    End Sub

    Public Sub RemovePipingRelatedVariables(ByVal oDoc As SolidEdge.Part.Interop.PartDocument)


        Try

            Dim objVariableList As SolidEdge.Framework.Interop.VariableList = Nothing
            Dim objVariables As SolidEdge.Framework.Interop.Variables = Nothing

            objVariables = objDoc.Variables

            objVariableList = objVariables.Query("LL")

            If objVariableList.Count = 1 Then
                objVariableList.Item(1).delete()
            End If

            Garbage_Collect(objVariableList)
            objVariableList = objVariables.Query("Diameter")

            If objVariableList.Count = 1 Then
                objVariableList.Item(1).delete()
            End If

            Garbage_Collect(objVariableList)


            objVariableList = objVariables.Query("Diameter_1")

            If objVariableList.Count = 1 Then
                objVariableList.Item(1).delete()
            End If

            Garbage_Collect(objVariableList)


            objVariableList = objVariables.Query("Diameter_2")

            If objVariableList.Count = 1 Then
                objVariableList.Item(1).delete()
            End If

            Garbage_Collect(objVariableList)

            objVariableList = objVariables.Query("Diameter_3")

            If objVariableList.Count = 1 Then
                objVariableList.Item(1).delete()
            End If

            Garbage_Collect(objVariableList)

            objVariableList = objVariables.Query("Diameter_4")

            If objVariableList.Count = 1 Then
                objVariableList.Item(1).delete()
            End If

            Garbage_Collect(objVariableList)
            Garbage_Collect(objVariables)
            Garbage_Collect(oDoc)
        Catch ex As Exception
            'raise string5
            'raise string4
            MessageBox.Show(string5, string8, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
            End
        End Try

    End Sub

    Private Sub objCmd_Activate() Handles objCmd.Activate

    End Sub

    Private Sub objCmd_Terminate() Handles objCmd.Terminate

    End Sub

    Private Sub RBCross_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RBCross.CheckedChanged

        Me.CBxCommonDiameter2Angled.Checked = False
        Me.CBx2AngledDiffDiameter.Checked = False
        Me.TxtDiameter1.Enabled = False
        Me.TxtDiameter2.Enabled = False
        Me.TxtDiameter3.Enabled = False
        Me.TxtDiameter4.Enabled = False


        If Me.RBCross.Checked = True Then
            Me.Label1.Text = string15

            Me.Label1.Refresh()
            objApp.StatusBar = string15
        End If
    End Sub

    Private Sub CBxCommonDiameter2Angled_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBxCommonDiameter2Angled.CheckedChanged
        If Me.RBValve.Checked = True Or Me.RB_Elbow.Checked = True Then
            If CBxCommonDiameter2Angled.Checked = True Then
                Me.TxtDiameter1.Enabled = True
                Me.TxtDiameter2.Enabled = False
                Me.TxtDiameter3.Enabled = False
                Me.TxtDiameter4.Enabled = False
                CBx2AngledDiffDiameter.Checked = False
            End If
        End If

        If Me.RBTee.Checked = True Then
            If CBxCommonDiameter2Angled.Checked = True Then
                Me.TxtDiameter1.Enabled = True
                Me.TxtDiameter2.Enabled = False
                Me.TxtDiameter3.Enabled = False
                Me.TxtDiameter4.Enabled = False
                CBx2AngledDiffDiameter.Checked = False
            End If
        End If


        If Me.RBCross.Checked = True Then
            If CBxCommonDiameter2Angled.Checked = True Then
                Me.TxtDiameter1.Enabled = True
                Me.TxtDiameter2.Enabled = False
                Me.TxtDiameter3.Enabled = False
                Me.TxtDiameter4.Enabled = False
                CBx2AngledDiffDiameter.Checked = False
            End If

        End If



    End Sub

    Private Sub CBx2AngledDiffDiameter_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBx2AngledDiffDiameter.CheckedChanged

        If Me.RBPipe.Checked = True Then
            Me.CBx2AngledDiffDiameter.Checked = False
        End If



        If Me.RBValve.Checked = True Or Me.RB_Elbow.Checked = True Then
            If CBx2AngledDiffDiameter.Checked = True Then
                Me.TxtDiameter1.Enabled = True
                Me.TxtDiameter2.Enabled = True
                Me.TxtDiameter3.Enabled = False
                Me.TxtDiameter4.Enabled = False
                Me.CBxCommonDiameter2Angled.Checked = False
            End If
        End If

        If Me.RBTee.Checked = True Then
            If CBx2AngledDiffDiameter.Checked = True Then
                Me.TxtDiameter1.Enabled = True
                Me.TxtDiameter2.Enabled = True
                Me.TxtDiameter3.Enabled = True
                Me.TxtDiameter4.Enabled = False
                Me.CBxCommonDiameter2Angled.Checked = False
            End If
        End If


        If Me.RBCross.Checked = True Then
            If CBx2AngledDiffDiameter.Checked = True Then
                Me.TxtDiameter1.Enabled = True
                Me.TxtDiameter2.Enabled = True
                Me.TxtDiameter3.Enabled = True
                Me.TxtDiameter4.Enabled = True
                Me.CBxCommonDiameter2Angled.Checked = False
            End If

        End If





    End Sub

    Public Sub AddToVariabletable(ByVal STRVariableName As String, ByVal strValue As String)


        Dim objVariables As SolidEdge.Framework.Interop.Variables = Nothing

        Dim objVariableList As SolidEdge.Framework.Interop.VariableList = Nothing

        Dim ii As Integer
        Dim VarName As String

        Try
            objVariables = objDoc.Variables
            objVariableList = objVariables.Query(STRVariableName)
            For ii = 1 To objVariableList.Count
                VarName = objVariableList.Item(ii).name
                If VarName = "IsFlange" Then
                    objVariableList.Item(ii).value = CLng(strValue)
                Else
                    objVariableList.Item(ii).value = objApp.ActiveDocument.unitsofmeasure.parseunit(SolidEdge.Constant.Interop.UnitTypeConstants.igUnitDistance, strValue)
                End If

            Next ii
            If objVariableList.Count = 0 Then

                If STRVariableName = "IsFlange" Then
                    objVariables.Add(STRVariableName, strValue, SolidEdge.Constant.Interop.UnitTypeConstants.igUnitScalar)
                Else
                    objVariables.Add(STRVariableName, strValue, SolidEdge.Constant.Interop.UnitTypeConstants.igUnitDistance)
                End If

            End If
        Catch ex As Exception

        End Try

        Garbage_Collect(objVariableList)
        Garbage_Collect(objVariables)




    End Sub

    Private Sub RBPipe_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RBPipe.CheckedChanged
        Me.CBxCommonDiameter2Angled.Checked = False
        Me.CBx2AngledDiffDiameter.Checked = False
        Me.TxtDiameter1.Enabled = False
        Me.TxtDiameter2.Enabled = False
        Me.TxtDiameter3.Enabled = False
        Me.TxtDiameter4.Enabled = False




        If Me.RBPipe.Checked = True Then
            Me.CBxCommonDiameter2Angled.Checked = True
            Me.TxtDiameter1.Enabled = True
            Me.Label1.Text = string16
            Me.Label1.Refresh()
            objApp.StatusBar = string16
        End If

    End Sub
End Class
