Reply

Adding planar align between assembly and part

[ Edited ]

 

Hi all

I'm trying to add an assembly and a part into a top level assembly in ST and create a planar align between a blue face in the assembly and a red face in the part (created using part painter).  I've copied the code below from the Solid Edge Programming guide and modified it slightly.

So far I can assign the correct faces to objFace1 and objFace2 (from their colours) and create a reference to the face in the part, but I can't create a reference to the face in the assembly and I can't find any examples of the AddPlanar method.

I've found similar problems on forums, but no solutions.  I've also been using the Solid Edge Spy which has been a massive help to get this far!

Any advice would be much appreciated.

Cheers

Nick

 

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

        Dim objApplication As SolidEdgeFramework.Application = Nothing

        Dim objDocuments As SolidEdgeFramework.Documents = Nothing

        Dim objAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing

        Dim objOccurrences As SolidEdgeAssembly.Occurrences = Nothing

        Dim objOccurrence1 As SolidEdgeAssembly.Occurrence = Nothing

        Dim objOccurrence2 As SolidEdgeAssembly.Occurrence = Nothing

        Dim objPart As SolidEdgePart.PartDocument = Nothing

        Dim objModels As SolidEdgePart.Models = Nothing

        Dim objModel As SolidEdgePart.Model = Nothing

        Dim objFaces As SolidEdgeGeometry.Faces = Nothing

        Dim objFace As SolidEdgeGeometry.Face = Nothing

        Dim objRelations3d As SolidEdgeAssembly.Relations3d = Nothing

        Dim objGroundRel As SolidEdgeAssembly.GroundRelation3d = Nothing

        Dim objRel As SolidEdgeAssembly.AxialRelation3d = Nothing

        Dim objRefFace1 As SolidEdgeFramework.Reference = Nothing

        Dim objRefFace2 As SolidEdgeFramework.Reference = Nothing

        Dim objRefAxial1 As SolidEdgeFramework.Reference = Nothing

        Dim objRefAxial2 As SolidEdgeFramework.Reference = Nothing

        Dim objFace1 As SolidEdgeGeometry.Face = Nothing

        Dim objFace2 As SolidEdgeGeometry.Face = Nothing

        Dim objProtrusion As SolidEdgePart.ExtrudedProtrusion

        Dim objSubOccurrence As SolidEdgeAssembly.SubOccurrence

        Dim objSubOccurrence1 As SolidEdgeAssembly.SubOccurrence

 

        Try

            objApplication = Marshal.GetActiveObject("SolidEdge.Application")

 

            objDocuments = objApplication.Documents

 

            objAssembly = objDocuments.Add("SolidEdge.AssemblyDocument")

 

            objOccurrences = objAssembly.Occurrences

 

            objOccurrence1 = objOccurrences.AddByFilename("C:\CFR4_1.asm")

 

            For Each objSubOccurrence In objOccurrence1.SubOccurrences

                For Each objModel In objSubOccurrence.SubOccurrenceDocument.Models

                    For Each objProtrusion In objModel.ExtrudedProtrusions

                        For Each objFace In objProtrusion.SideFaces

                            If objFace.Style IsNot Nothing Then

                                If objFace.Style.StyleName = "Blue" Then

                                    objFace1 = objFace

                                    objSubOccurrence1 = objSubOccurrence

                                End If

                            End If

                        Next

                    Next

                Next

            Next

 

            objRefFace1 = objAssembly.CreateReference(objOccurrence1, objFace1) 'Doesn't Workfor Assembly

 

            objOccurrence2 = objOccurrences.AddByFilename("C:\0-L0014_0.par")

 

            objPart = objOccurrence2.OccurrenceDocument

 

            objModels = objPart.Models

 

            For Each objModel In objModels

                For Each objProtrusion In objModel.ExtrudedProtrusions

                    For Each objFace In objProtrusion.SideFaces

                        If objFace.Style IsNot Nothing Then

                            If objFace.Style.StyleName = "Red" Then

                                objFace2 = objFace

                            End If

                        End If

                    Next

                Next

            Next

 

            objRefFace2 = objAssembly.CreateReference(objOccurrence2, objFace2)

 

            objRelations3d = objAssembly.Relations3d

            objGroundRel = objRelations3d.Item(2)

            objGroundRel.Delete()

 

            Dim Constrain1 As System.Array '?

            Dim Constrain2 As System.Array '?

 

            objRel = objRelations3d.AddPlanar(objRefFace1, objRefFace2, False, Constrain1, Constrain2) '?

 

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        Finally

            If Not (objRel Is Nothing) Then

                Marshal.ReleaseComObject(objRel)

                objRel = Nothing

            End If

            If Not (objGroundRel Is Nothing) Then

                Marshal.ReleaseComObject(objGroundRel)

                objGroundRel = Nothing

            End If

            If Not (objRelations3d Is Nothing) Then

                Marshal.ReleaseComObject(objRelations3d)

                objRelations3d = Nothing

            End If

            If Not (objRefFace1 Is Nothing) Then

                Marshal.ReleaseComObject(objRefFace1)

                objRefFace1 = Nothing

            End If

            If Not (objRefFace1 Is Nothing) Then

                Marshal.ReleaseComObject(objRefFace1)

                objRefFace1 = Nothing

            End If

            If Not (objFace1 Is Nothing) Then

                Marshal.ReleaseComObject(objFace1)

                objFace1 = Nothing

            End If

            If Not (objFace2 Is Nothing) Then

                Marshal.ReleaseComObject(objFace2)

                objFace2 = Nothing

            End If

            If Not (objFace Is Nothing) Then

                Marshal.ReleaseComObject(objFace)

                objFace = Nothing

            End If

            If Not (objFaces Is Nothing) Then

                Marshal.ReleaseComObject(objFaces)

                objFaces = Nothing

            End If

            If Not (objModel Is Nothing) Then

                Marshal.ReleaseComObject(objModel)

                objModel = Nothing

            End If

            If Not (objModels Is Nothing) Then

                Marshal.ReleaseComObject(objModels)

                objModels = Nothing

            End If

            If Not (objPart Is Nothing) Then

                Marshal.ReleaseComObject(objPart)

                objPart = Nothing

            End If

            If Not (objOccurrence2 Is Nothing) Then

                Marshal.ReleaseComObject(objOccurrence2)

                objOccurrence2 = Nothing

            End If

            If Not (objOccurrence1 Is Nothing) Then

                Marshal.ReleaseComObject(objOccurrence1)

                objOccurrence1 = Nothing

            End If

            If Not (objOccurrences Is Nothing) Then

                Marshal.ReleaseComObject(objOccurrences)

                objOccurrences = Nothing

            End If

            If Not (objAssembly Is Nothing) Then

                Marshal.ReleaseComObject(objAssembly)

                objAssembly = Nothing

            End If

            If Not (objDocuments Is Nothing) Then

                Marshal.ReleaseComObject(objDocuments)

                objDocuments = Nothing

            End If

            If Not (objApplication Is Nothing) Then

                Marshal.ReleaseComObject(objApplication)

                objApplication = Nothing

            End If

        End Try

End Sub

 

Posted by: Nick Whitehead
Post date: 11/4/2008 5:02:24 AM

1 REPLY

RE: Adding planar align between assembly and part

[ Edited ]

 

I've finally managed to fix the problems I was having, so for anyone who's interested, I've pasted the code below.  It's a bit messy, but I'll clean it up now I know it works!

The code adds a planar align between the blue and red faces in the assembly and part respectively and adds two axial aligns to the green holes and white holes.

I am working on a quick way to create a manifold assembly (which basically consists of a range of valves bolted together with tie rods in thousands of different possible configurations).

The intention is to list a series of parts and assemblies (with their mating colours defined) and have VB create the assembly from scratch, then automatically create a STEP file and draft to send to customers (if this is possible, I'll face that problem when I get to it!).

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

        Dim objApplication As SolidEdgeFramework.Application = Nothing

        Dim objDocuments As SolidEdgeFramework.Documents = Nothing

        Dim objAssembly As SolidEdgeAssembly.AssemblyDocument = Nothing

        Dim objPart As SolidEdgePart.PartDocument = Nothing

        Dim objHole As SolidEdgePart.Hole = Nothing

        Dim objProtrusion As SolidEdgePart.ExtrudedProtrusion = Nothing

 

        Dim objRelations3d As SolidEdgeAssembly.Relations3d = Nothing

        Dim objGroundRel As SolidEdgeAssembly.GroundRelation3d = Nothing

 

        Dim objOccurrences As SolidEdgeAssembly.Occurrences = Nothing

        Dim objOccurrenceDoc As SolidEdgeAssembly.AssemblyDocument = Nothing

        Dim objOccurrence1 As SolidEdgeAssembly.Occurrence = Nothing

        Dim objOccurrence2 As SolidEdgeAssembly.Occurrence = Nothing

 

        Dim objSubOccurrences As SolidEdgeAssembly.Occurrences = Nothing

        Dim objSubOccurrence As SolidEdgeAssembly.Occurrence = Nothing

        Dim objSubOccurrence1 As SolidEdgeAssembly.Occurrence = Nothing

        Dim ObjSubOccurrenceDoc1 As SolidEdgePart.PartDocument = Nothing

        Dim objSubOccurrence2 As SolidEdgeAssembly.Occurrence = Nothing

        Dim ObjSubOccurrenceDoc2 As SolidEdgePart.PartDocument = Nothing

 

        Dim objModels As SolidEdgePart.Models = Nothing

        Dim objModel As SolidEdgePart.Model = Nothing

 

        Dim objFaces As SolidEdgeGeometry.Faces = Nothing

        Dim objFace As SolidEdgeGeometry.Face = Nothing

 

        Dim objPlanarFace1 As SolidEdgeGeometry.Face = Nothing

        Dim objPlanarFace2 As SolidEdgeGeometry.Face = Nothing

 

        Dim objAxialFace1_1 As SolidEdgeGeometry.Face = Nothing

        Dim objAxialFace2_1 As SolidEdgeGeometry.Face = Nothing

 

        Dim objAxialFace1_2 As SolidEdgeGeometry.Face = Nothing

        Dim objAxialFace2_2 As SolidEdgeGeometry.Face = Nothing

 

        Dim objRefPlanar1 As SolidEdgeFramework.Reference = Nothing

        Dim objRefPlanar2 As SolidEdgeFramework.Reference = Nothing

 

        Dim objRefAxialFace1_1 As SolidEdgeFramework.Reference = Nothing

        Dim objRefAxialFace2_1 As SolidEdgeFramework.Reference = Nothing

 

        Dim objRefAxialFace1_2 As SolidEdgeFramework.Reference = Nothing

        Dim objRefAxialFace2_2 As SolidEdgeFramework.Reference = Nothing

 

        Dim objRefSubOccurrence1 As SolidEdgeFramework.Reference = Nothing

        Dim objRefSubOccurrence2 As SolidEdgeFramework.Reference = Nothing

        Dim objRefSubOccurrence1_1 As SolidEdgeFramework.Reference = Nothing

        Dim objRefSubOccurrence1_2 As SolidEdgeFramework.Reference = Nothing

        Dim objRefSubOccurrence2_1 As SolidEdgeFramework.Reference = Nothing

        Dim objRefSubOccurrence2_2 As SolidEdgeFramework.Reference = Nothing

 

        Dim objRelAxial1 As SolidEdgeAssembly.AxialRelation3d = Nothing

        Dim objRelAxial2 As SolidEdgeAssembly.AxialRelation3d = Nothing

        Dim objRelPlanar As SolidEdgeAssembly.PlanarRelation3d = Nothing

 

        Dim Constrain1(2) As Double

        Dim Constrain2(2) As Double

 

 

        objApplication = Marshal.GetActiveObject("SolidEdge.Application")

        objDocuments = objApplication.Documents

        objAssembly = objDocuments.Add("SolidEdge.AssemblyDocument")

        objOccurrences = objAssembly.Occurrences

        objOccurrence1 = objOccurrences.AddByFilename("C:\CFR4_1.asm")

 

        objOccurrenceDoc = objOccurrence1.OccurrenceDocument

 

        objSubOccurrences = objOccurrenceDoc.Occurrences

 

        For Each objSubOccurrence In objSubOccurrences

 

            For Each objModel In objSubOccurrence.OccurrenceDocument.models

                For Each objProtrusion In objModel.ExtrudedProtrusions

                    For Each objFace In objProtrusion.SideFaces

                        If objFace.Style IsNot Nothing Then

                            If Strings.UCase(Strings.Trim(objFace.Style.StyleName)) = "BLUE" Then

                                objPlanarFace1 = objFace

                                objSubOccurrence1 = objSubOccurrence

                                ObjSubOccurrenceDoc1 = objSubOccurrence.OccurrenceDocument

                            End If

                        End If

                    Next

                Next

                For Each objHole In objModel.Holes

                    For Each objFace In objHole.SideFaces

                        If objFace.Style IsNot Nothing Then

                            If Strings.UCase(Strings.Trim(objFace.Style.StyleName)) = "GREEN" Then

                                objAxialFace1_1 = objFace

                            ElseIf Strings.UCase(Strings.Trim(objFace.Style.StyleName)) = "WHITE" Then

                                objAxialFace1_2 = objFace

                            End If

                        End If

                    Next

                Next

            Next

        Next

 

        objRefSubOccurrence1 = objAssembly.CreateReference(objSubOccurrence1, objPlanarFace1)

        objRefPlanar1 = objAssembly.CreateReference(objOccurrence1, objRefSubOccurrence1)

 

        objRefSubOccurrence1_1 = objAssembly.CreateReference(objSubOccurrence1, objAxialFace1_1)

        objRefAxialFace1_1 = objAssembly.CreateReference(objOccurrence1, objRefSubOccurrence1_1)

 

        objRefSubOccurrence1_2 = objAssembly.CreateReference(objSubOccurrence1, objAxialFace1_2)

        objRefAxialFace1_2 = objAssembly.CreateReference(objOccurrence1, objRefSubOccurrence1_2)

 

        objOccurrence2 = objOccurrences.AddByFilename("C:\0-L0014_0.par")

 

        objPart = objOccurrence2.OccurrenceDocument

 

        objModels = objPart.Models

 

        For Each objModel In objModels

            For Each objProtrusion In objModel.ExtrudedProtrusions

                For Each objFace In objProtrusion.SideFaces

                    If objFace.Style IsNot Nothing Then

                        If Strings.UCase(Strings.Trim(objFace.Style.StyleName)) = "RED" Then

                            objPlanarFace2 = objFace

                        End If

                    End If

                Next

            Next

 

            For Each objHole In objModel.Holes

                For Each objFace In objHole.SideFaces

                    If objFace.Style IsNot Nothing Then

                        If Strings.UCase(Strings.Trim(objFace.Style.StyleName)) = "GREEN" Then

                            objAxialFace2_1 = objFace

                        ElseIf Strings.UCase(Strings.Trim(objFace.Style.StyleName)) = "WHITE" Then

                            objAxialFace2_2 = objFace

                        End If

                    End If

                Next

            Next

 

        Next

 

        objRefPlanar2 = objAssembly.CreateReference(objOccurrence2, objPlanarFace2)

 

        objRefAxialFace2_1 = objAssembly.CreateReference(objOccurrence2, objAxialFace2_1)

        objRefAxialFace2_2 = objAssembly.CreateReference(objOccurrence2, objAxialFace2_2)

 

 

        objRelations3d = objAssembly.Relations3d

        objGroundRel = objRelations3d.Item(2)

        objGroundRel.Delete()

 

        Constrain1(0) = 0

        Constrain1(1) = 0

        Constrain1(2) = 0

 

        Constrain2(0) = 0

        Constrain2(1) = 0

        Constrain2(2) = 0    'These values don't seem to matter too much.  I'm sure there's a reason for the two constraining points, but I can't find any!

 

        objRelPlanar = objRelations3d.AddPlanar(objRefPlanar1, objRefPlanar2, True, Constrain1, Constrain2)

 

        objRelAxial1 = objRelations3d.AddAxial(objRefAxialFace1_1, objRefAxialFace2_1, False)

        objRelAxial2 = objRelations3d.AddAxial(objRefAxialFace1_2, objRefAxialFace2_2, False)

 

    End Sub

 

Posted by: Nick Whitehead
Post date: 11/11/2008 3:10:33 AM