createReference() error.

Experimenter
Experimenter

Hello Solid Edge community.

 

I am stuck on a problem that seems absurd to me, I cannot understand why I get this error and I need to solve it. I am writing a bit of code using the API to control Solid Edge from the outside.

 

I have 8 components, all assemblies, and I need to assemble them in a certain order. Since every components goes on top of the previous one I thought it was best to create 2 Coordinates Systems for every components, an Upper and a Lower one, in order to match them appropriately. The Coordinate Systems have been created in the same way for every component, which will now have 3 CS: (1) its own default CS, (2) my Lower CS, (3) my Upper CS.

 

Starting from the first component, the second will go above it, so I will match the Upper CS (3) of the lower component with the Lower CS (2) of the upper components. For the next one I would do exactly the same.

 

I have connected the program to the istance of Solid Edge, I've created an Assembly and I've imported all the 8 components. The components need to be assembled in the same order as they've been imported, and this piece of code gets called from an outside function that passes it a value, "c", which increments every time. I use that value to know which components I need to mate (the first time it's 1 and 2, the second 2 and 3, and so on).

 

This is the Code I have been developing.

 

public void Join(Componente lower, Componente upper, int c)
        {
            SolidEdgeAssembly.AssemblyDocument Assy = null;
            SolidEdgeAssembly.Occurrence Occ1 = null, Occ2 = null;
            SolidEdgePart.CoordinateSystem Occ1COS = null, Occ2COS = null;
            SolidEdgeAssembly.Relations3d Rels3D = null;
            SolidEdgeFramework.Reference Occ1Ref, Occ2Ref;
            SolidEdgeGeometry.Face Occ1XYPlane = null, Occ1YZPlane = null, Occ1ZXPlane =null;
            SolidEdgeGeometry.Face Occ2XYPlane = null, Occ2YZPlane = null, Occ2ZXPlane = null;
            Object A1XO, A1YO, A1ZO, A1XR, A1YR, A1ZR;
            Object A2XO, A2YO, A2ZO, A2XR, A2YR, A2ZR;
            Double[] ConstrainingPoint1 = new double[6];
            Double[] ConstrainingPoint2 = new double[6];

            //Connect to the Assembly containing all the components
            Assy = application.ActiveDocument;

            //Create the occurrence fro the 2 components to be assembled. c=1 at first, it the increments every time
            Occ1 = Assy.Occurrences.Item(c);
            Occ2 = Assy.Occurrences.Item(c+1);

            //The first component has only one Coordinate Systems, since it can be assembled only with another component on top of it
            if (c == 1)
            {
                //Remove Ground Relationship to all components except the first one, that must stay fixed
                int a = 2;

                while (a <= Assy.Occurrences.Count)
                {
                    Assy.Occurrences.Item(a).Relations3d.item(1).Delete();
                    a++;
                }
                //Get the Coordinate System of the first component to be assembled
                Occ1COS = Occ1.OccurrenceDocument.CoordinateSystems.Item(2);
            }
            else
            {
                //Get the Coordinate System of the first component to be assembled
                Occ1COS = Occ1.OccurrenceDocument.CoordinateSystems.Item(3);
            }

            //Get the Coordinate System of the second component to be assembled
            Occ2COS = Occ2.OccurrenceDocument.CoordinateSystems.Item(2);

            //Get the details of the Coordinate Systems
            Occ1COS.GetOrientation(out A1XO, out A1YO, out A1ZO, out A1XR, out A1YR, out A1ZR);
            Occ2COS.GetOrientation(out A2XO, out A2YO, out A2ZO, out A2XR, out A2YR, out A2ZR);


            //Get the planes corrisponding to the Coordinate Systems
            Occ1XYPlane = Occ1COS.Plane[SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysXYPlane];
            Occ1YZPlane = Occ1COS.Plane[SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysYZPlane];
            Occ1ZXPlane = Occ1COS.Plane[SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysZXPlane];
            
            Occ2XYPlane = Occ2COS.Plane[SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysXYPlane];
            Occ2YZPlane = Occ2COS.Plane[SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysYZPlane];
            Occ2ZXPlane = Occ2COS.Plane[SolidEdgePart.CoordinateSystemFeatureConstants.seCoordSysZXPlane];


            //Create a reference between the component and its Coordinate System's plane
            Occ1Ref = null;
            Occ2Ref = null;

            Occ1Ref = Assy.CreateReference(Occ1, Occ1XYPlane);
            Occ2Ref = Assy.CreateReference(Occ2, Occ2XYPlane);

            //Add a planar relationship between the same 2 planes from the COS of the 2 components
            Assy.Relations3d.AddPlanar(Occ1Ref, Occ2Ref, false, ConstrainingPoint1, ConstrainingPoint2);
            
            Occ1Ref = null;
            Occ2Ref = null;

            Occ1Ref = Assy.CreateReference(Occ1, Occ1YZPlane);
            Occ2Ref = Assy.CreateReference(Occ2, Occ2YZPlane);
            Assy.Relations3d.AddPlanar(Occ1Ref, Occ2Ref, false, ConstrainingPoint1, ConstrainingPoint2);

            Occ1Ref = null;
            Occ2Ref = null;

            Occ1Ref = Assy.CreateReference(Occ1, Occ1ZXPlane);
            Occ2Ref = Assy.CreateReference(Occ2, Occ2ZXPlane);
            Assy.Relations3d.AddPlanar(Occ1Ref, Occ2Ref, false, ConstrainingPoint1, ConstrainingPoint2);
        }

 

Now, for the first 2 components the code is a piece of cake, it works like a charm. The problem is when I try to repeat the code and assemble the second and third components.

 

At this point, Occ1 = second component, Occ2 = third component and I will match them using Occ1.OccurrenceDocument.CoordinateSystems.Item(3) and Occ2.OccurrenceDocument.CoordinateSystems.Item(2)

 

I get an error when I try to create the second reference for the Occ1 , basically here

 

            Occ1Ref = Assy.CreateReference(Occ1, Occ1YZPlane);

 

The error, which is generic and tells me nothing, is the following 

 

err.jpg

 

 

The STRANGE thing is that it ALWAYS get stuck on the second reference I try to create for the Occurrence 1. It does not matter which it is, it's always the second one. So if I follow this order "XY-YZ-ZX" it crashes on YZ, but I follow "YZ-XY-ZX" it crashes on XY.

 

What is the issue? I have tried everything I can think of but I cannot get past that second reference creation. Am I missing something?

 

Can someone help me? I've been struggling with this for days and I cannot find a reason why I should not be able to create that reference...

 

I have attached the first 3 components I am trying to mate.

 

Thank you. 

5 REPLIES

Re: createReference() error.

Valued Contributor
Valued Contributor

Are you disposing the COM objects every time you use them? if not the second time you use them the computer does not know which one to use, if there are two or more.

Re: createReference() error.

Experimenter
Experimenter

How would I do that?

 

I am setting the Reference object to null after time I add a planar relationship, is that enough?

Re: createReference() error.

Valued Contributor
Valued Contributor

No is not enough, you need to add this code, and send the COM object after you used, don't set it to null, becasue then you can't relese it, you need to make sure it gets released.

 

public static void releaseObject(object objApp)

{

if (objApp != null)

{

try

{

Marshal.ReleaseComObject(objApp);

objApp = null;

}

catch (System.Exception ex)

{

Console.Write(ex.Message);

}

}

}

 

 

Re: createReference() error.

Experimenter
Experimenter

Thank you, I have modified the code and now I am releasing all the COM object, but I am still getting the same error.

 

err.jpg

Re: createReference() error.

Valued Contributor
Valued Contributor

after you us them don;t set the COM Objects to nulll, just release them. Also see how many instances of Edge.exer are running on the task manager it should be only one.