private void CreateInvLayout() { CreateLayoutPartFile(); Inventor.AssemblyComponentDefinition componentDefinition = InventorServices.Persistence.PersistenceManager.ActiveAssemblyDoc.ComponentDefinition; TransformationMatrix = InventorApplication.TransientGeometry.CreateMatrix(); ComponentOccurrence componentOccurrence = componentDefinition.Occurrences.Add(LayoutPartPath, TransformationMatrix); ComponentOccurrences occurrences = componentDefinition.Occurrences; //TODO This is janky. Don't need to assume that we are starting in an empty assembly file. ComponentOccurrence layoutOccurrence = occurrences[1]; PartComponentDefinition layoutComponentDefinition = (PartComponentDefinition)layoutOccurrence.Definition; for (int i = 0; i < ModulePoints.Count; i++) { WorkPoint workPoint = layoutComponentDefinition.WorkPoints.AddFixed(ModulePoints[i], false); workPoint.Grounded = true; workPoint.Visible = false; //Inventor's API documentation is so bad! object workPointProxyObject; layoutOccurrence.CreateGeometryProxy(workPoint, out workPointProxyObject); LayoutWorkPointProxies.Add((WorkPointProxy)workPointProxyObject); LayoutWorkPoints.Add(workPoint); } LayoutWorkPlane = layoutComponentDefinition.WorkPlanes.AddByThreePoints(layoutWorkPoints[0], layoutWorkPoints[1], layoutWorkPoints[2]); LayoutWorkPlane.Grounded = true; LayoutWorkPlane.Visible = false; object wPlaneProxyObject; layoutOccurrence.CreateGeometryProxy(LayoutWorkPlane, out wPlaneProxyObject); ModuleWorkPlaneProxyAssembly = (WorkPlaneProxy)wPlaneProxyObject; }
internal void PlaceWorkGeometryForContsraints(PartComponentDefinition layoutComponentDefinition, ComponentOccurrence layoutOccurrence, int moduleNumber) { PartDocument partDoc = (PartDocument)layoutComponentDefinition.Document; ReferenceKeyManager refKeyManager = partDoc.ReferenceKeyManager; //TODO: Property ModuleId can be factored out and _binder.ContextData can be used instead. ModuleId = moduleNumber; for (int i = 0; i < PointObjects.Count; i++) { //Each ModuleObject needs to have its ContextManager set. PointObjects[i].Binder.ContextManager.BindingContextManager = refKeyManager; WorkPoint workPoint; if (PointObjects[i].Binder.GetObjectFromTrace <WorkPoint>(out workPoint)) { Inventor.Point newLocation = PersistenceManager.InventorApplication.TransientGeometry.CreatePoint(InternalModulePoints[i].X, InternalModulePoints[i].Y, InternalModulePoints[i].Z); workPoint.SetFixed(InternalModulePoints[i].ToPoint()); } else { workPoint = layoutComponentDefinition.WorkPoints.AddFixed(InternalModulePoints[i].ToPoint(), false); PointObjects[i].Binder.SetObjectForTrace <WorkPoint>(workPoint, ModuleUtilities.ReferenceKeysSorter); } //workPoint.Visible = false; object workPointProxyObject; layoutOccurrence.CreateGeometryProxy(workPoint, out workPointProxyObject); LayoutWorkPointProxies.Add((WorkPointProxy)workPointProxyObject); LayoutWorkPoints.Add(workPoint); } //If we will have more than 2 constraints, it will help assembly stability later //if we have a plane to constrain to first. if (InternalModulePoints.Count > 2) { WorkPlane workPlane; //TODO: Is this a good idea? Why is this a list? Will we ever have more //than work plane? PlaneObjects[0].Binder.ContextManager.BindingContextManager = refKeyManager; if (PlaneObjects[0].Binder.GetObjectFromTrace <WorkPlane>(out workPlane)) { if (workPlane.DefinitionType == WorkPlaneDefinitionEnum.kThreePointsWorkPlane) { workPlane.SetByThreePoints(LayoutWorkPoints[0], LayoutWorkPoints[1], LayoutWorkPoints[2]); LayoutWorkPlane = workPlane; object wPlaneProxyObject; layoutOccurrence.CreateGeometryProxy(workPlane, out wPlaneProxyObject); LayoutWorkPlaneProxy = (WorkPlaneProxy)wPlaneProxyObject; } } else { //If the first three points are colinear, adding a workplane will fail. We will check the area of a triangle //described by the first three points. If the area is very close to 0, we can assume these points are colinear, and we should //not attempt to construct a work plane from them. Inventor.Point pt1 = LayoutWorkPoints[0].Point; Inventor.Point pt2 = LayoutWorkPoints[1].Point; Inventor.Point pt3 = LayoutWorkPoints[2].Point; if (Math.Abs(pt1.X * (pt2.Y - pt3.Y) + pt2.X * (pt3.Y - pt1.Y) + pt3.X * (pt1.Y - pt2.Y)) > .0000001) { workPlane = layoutComponentDefinition.WorkPlanes.AddByThreePoints(LayoutWorkPoints[0], LayoutWorkPoints[1], LayoutWorkPoints[2], false); PlaneObjects[0].Binder.SetObjectForTrace <WorkPlane>(workPlane, ModuleUtilities.ReferenceKeysSorter); workPlane.Grounded = true; //workPlane.Visible = false; LayoutWorkPlane = workPlane; object wPlaneProxyObject; layoutOccurrence.CreateGeometryProxy(workPlane, out wPlaneProxyObject); LayoutWorkPlaneProxy = (WorkPlaneProxy)wPlaneProxyObject; } } } }