Cancel
Showing results for 
Search instead for 
Did you mean: 

Automation for match two parts with user system coordinate

Experimenter
Experimenter

Hi folks,

 

I need to match two parts using API in VB net or in c#.

 

Actually I can import parts inside the assembly ambient, but I can't position with coordinate system i have create in part files.

 

// Connect to a running instance of Solid Edge
                 application = (SolidEdgeFramework.Application) Marshal.GetActiveObject("SolidEdge.Application");
                 // Get a reference to the documents collection
                 documents = application.Documents;
                 // Create a new assembly document
                 assembly = (SolidEdgeAssembly.AssemblyDocument)
                 documents.Add("SolidEdge.AssemblyDocument", Missing.Value);
                 // Get a reference to the occurrences collection
                 occurrences = assembly.Occurrences;
                 // Add the first block to the assembly
                 occurrence1 = occurrences.AddByFilename(@"C:\Cubo.par",
Missing.Value);
                 // Add the second block to the assembly
                 occurrence2 =
occurrences.AddByFilename(@"C:\Pernetto.par", Missing.Value);
                 SolidEdgePart.PartDocument first = occurrence1.PartDocument;
                 SolidEdgePart.CoordinateSystems first_cs = first.CoordinateSystems;
                 SolidEdgePart.CoordinateSystem cs1 = first_cs.Item(2);

                 SolidEdgePart.PartDocument second = occurrence2.PartDocument;

                 SolidEdgePart.CoordinateSystems second_cs = second.CoordinateSystems;

                 SolidEdgePart.CoordinateSystem cs2 = second_cs.Item(2);

I would like to make the user system coordinate "cs2" on the "cs1"  .

 

Any help is appreciate!

thanks

 

2 REPLIES 2

Re: Automation for match two parts with user system coordinate

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

Hi @Procedi 

 

This is an extract of an old project i made for a customer. It is a routine to create a relation between 2 coord systems that exist in two different parts in an assembly.

In fact, it creates 3 planar relations between the coordinate systems XY planes, YZ planes and ZX planes, so both parts result coincident by the coordinate systems. 

Some names are in Spanish but it is quite understandable. I added some comments in English.

It is VB.net, you can translate it into C#

Regards,

    ''' <summary>
    ''' Create 3 relations between 2 coordinate systems, so they mate completely.
    ''' </summary>
    ''' <param name="asm">Assembly where the relations are being created</param>
    ''' <param name="ocu1">Occurrence 1, inside this there is a coordinate system CS1</param>
    ''' <param name="nombreCS1">Name of the coordinate system in occurrence 1</param>
    ''' <param name="ocu2">Occurrence 2, inside this there is a coordinate system CS1</param>
    ''' <param name="nombreCS2">Name of the coordinate system to mate in occurrence 1</param>
    ''' <returns></returns>
    Function crearRels2CSPlanos(ByVal asm As SolidEdgeAssembly.AssemblyDocument, ByVal ocu1 As SolidEdgeAssembly.Occurrence, ByVal nombreCS1 As String, ByVal ocu2 As SolidEdgeAssembly.Occurrence, ByVal nombreCS2 As String) As Integer
        Dim rel As SolidEdgeAssembly.PlanarRelation3d
        Dim cs1 As SolidEdgePart.CoordinateSystem, cs2 As SolidEdgePart.CoordinateSystem
        Dim ref1 As SolidEdgeFramework.Reference, ref2 As SolidEdgeFramework.Reference
        Dim cp1(0 To 2) As Double, cp2(0 To 2) As Double
        crearRels2CSPlanos = 0
        'Get cs1
        cs1 = buscarCSPorNombre(ocu1.OccurrenceDocument, nombreCS1)
        If cs1 Is Nothing Then
            crearRels2CSPlanos = 1
            Exit Function
        End If
        'Get cs2
        cs2 = buscarCSPorNombre(ocu2.OccurrenceDocument, nombreCS2)
        If cs2 Is Nothing Then
            crearRels2CSPlanos = 2
            Exit Function
        End If
        'Create reference from occurrence 1 to the XY plane of the coordinate system 1.
        'Sometimes it was throwing an uncontrolled error (don't know why) solved trying twice.
        Try
            ref1 = asm.CreateReference(ocu1, cs1.Plane(SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysXYPlane))
        Catch
            ref1 = asm.CreateReference(ocu1, cs1.Plane(SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysXYPlane))
        End Try

        'Create reference from occurrence 2 to the XY plane of the coordinate system 2.
        ref2 = asm.CreateReference(ocu2, cs2.Plane(SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysXYPlane))

        'Create a planar relation between both.
        rel = asm.Relations3d.AddPlanar(ref1, ref2, False, cp1, cp2)
        'Some error checking...
        If rel Is Nothing Then crearRels2CSPlanos = 4
        If rel.Status = SolidEdgeConstants.Relation3dStatusConstants.igRelation3dStatusUnsolved Then crearRels2CSPlanos = 4
        ref1 = Nothing
        ref2 = Nothing

        'Same. Create both references for YZ coord system plane, create the relation between both.
        ref1 = asm.CreateReference(ocu1, cs1.Plane(SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysYZPlane))
        ref2 = asm.CreateReference(ocu2, cs2.Plane(SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysYZPlane))
        rel = asm.Relations3d.AddPlanar(ref1, ref2, False, cp1, cp2)
        If rel Is Nothing Then crearRels2CSPlanos = 5
        If rel.Status = SolidEdgeConstants.Relation3dStatusConstants.igRelation3dStatusUnsolved Then crearRels2CSPlanos = 5
        ref1 = Nothing
        ref2 = Nothing

        'Id.id for ZX plane
        ref1 = asm.CreateReference(ocu1, cs1.Plane(SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysZXPlane))
        ref2 = asm.CreateReference(ocu2, cs2.Plane(SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysZXPlane))
        rel = asm.Relations3d.AddPlanar(ref1, ref2, False, cp1, cp2)
        If rel Is Nothing Then crearRels2CSPlanos = 6
        If rel.Status = SolidEdgeConstants.Relation3dStatusConstants.igRelation3dStatusUnsolved Then crearRels2CSPlanos = 6

        'Cleaning
        Marshal.ReleaseComObject(ref1)
        Marshal.ReleaseComObject(ref2)
        Marshal.ReleaseComObject(rel)
        Marshal.ReleaseComObject(cs1)
        Marshal.ReleaseComObject(cs2)

        rel = Nothing
        ref1 = Nothing
        ref2 = Nothing
        cs1 = Nothing
        cs2 = Nothing

        GC.Collect(GC.MaxGeneration)
        GC.WaitForPendingFinalizers()
        GC.Collect(GC.MaxGeneration)
    End Function

    'Find a Coordinate system by name in a part file. Used in routine above.
    Function buscarCSPorNombre(ByVal doc As SolidEdgePart.PartDocument, ByVal nombreCS As String) As SolidEdgePart.CoordinateSystem
        'Dim cs As SolidEdgePart.CoordinateSystem
        buscarCSPorNombre = Nothing
        For Each cs As SolidEdgePart.CoordinateSystem In doc.CoordinateSystems
            If StrComp(cs.DisplayName, nombreCS, vbTextCompare) = 0 Then
                buscarCSPorNombre = cs
                Exit Function
            End If
        Next
    End Function
Julian Guillo
Pixel Sistemas SL
Spain
Highlighted

Re: Automation for match two parts with user system coordinate

Valued Contributor
Valued Contributor

You need to mate the planes from the coordinate system not the coordinate system itself. (this is what SE does when you use that command in the GUI) 

 

You can get the plane using the following code

dim objPlane as SolidEdgeGeometry.face 
objPlane = cs2.Plane(CoordinateSystemFeatureConstants.seCoordSysXYPlane)

Repeating for yz and zx planes 

A reference must then be created for each plane using the plane and the occurrence it came from 

Dim refToCoordPlane1 As SolidEdgeFramework.Reference = topLevelDoc.CreateReference(occurrence1, objPlane)

Then using the follow line for each mate

        Dim pnt1(2) As Double
        Dim pnt2(2) As Double
        topLevelAssemblyDoc.Relations3d.AddPlanar(refToCoordPlane1, refToCoordPlane4, False, pnt1, pnt2)

I do not know what the two doubles do, the documentation says that that parameter "Specifies a point on the face of the first [or second] part" but the code I have uses what is shown above. topLevelAssemblyDoc is the assembly document. 

Hope that helps.