////////////////////////////////////////////////////////////////////////////////////////////// // // ////////////////////////////////////////////////////////////////////////////////////////////// void InsertAsSurfaceGraphics(Matrix transfo) { Inventor.Application InvApp = AdnInventorUtilities.InvApplication; Document document = InvApp.ActiveDocument; ComponentDefinition compDef = AdnInventorUtilities.GetCompDefinition(document); _clientGraphicsMng.SetGraphicsSource(InvApp.ActiveDocument); Dictionary <SurfaceBody, SurfaceBody> surfaceBodies = AdnInventorUtilities.GetTransientBodies( AdnInventorUtilities.GetCompDefinition(_componentDocument)); foreach (KeyValuePair <SurfaceBody, SurfaceBody> pair in surfaceBodies) { SurfaceGraphics surfGraph = _clientGraphicsMng.DrawSurface(pair.Value); GraphicsNode node = surfGraph.Parent; node.Transformation = transfo; node.Selectable = true; StyleSourceTypeEnum styleSourceType; node.RenderStyle = pair.Key.GetRenderStyle(out styleSourceType); } }
////////////////////////////////////////////////////////////////////////////////////////////// // Create a list of GraphicsNode, sliced by the plane argument and set their visibility. // A list of nodes is used here, so we can attribute each node a different render style // if needed. // ////////////////////////////////////////////////////////////////////////////////////////////// List <GraphicsNode> CreateSlicedNodes(Plane plane, bool visibility) { List <GraphicsNode> nodes = new List <GraphicsNode>(); foreach (KeyValuePair <SurfaceBody, SurfaceBody> pair in _surfaceBodies) { SurfaceGraphics surfGraph = _clientGraphicsMng.DrawSurface(pair.Value); GraphicsNode node = surfGraph.Parent; StyleSourceTypeEnum styleSourceType; node.RenderStyle = pair.Key.GetRenderStyle(out styleSourceType); nodes.Add(node); } ObjectCollection slicingPlanes = AdnInventorUtilities.InvApplication.TransientObjects.CreateObjectCollection(null); slicingPlanes.Add(plane); foreach (GraphicsNode node in nodes) { node.SliceGraphics(true, slicingPlanes, false, null); node.Visible = visibility; node.Selectable = true; } return(nodes); }
////////////////////////////////////////////////////////////////////////////////////////////// // Draws dimension graphics // ////////////////////////////////////////////////////////////////////////////////////////////// DimData DrawDimension(Point point1, Point point2, Point dimText, Vector normal) { // Compute extension points Vector xAxis = point1.VectorTo(point2); Vector upVector = normal.CrossProduct(xAxis); upVector.Normalize(); Plane plane = _Tg.CreatePlane(point1, normal); Point dimTextProj = AdnInventorUtilities.ProjectOnPlane(dimText, plane); double dotP1 = point1.VectorTo(dimTextProj).DotProduct(upVector); double dotP2 = point2.VectorTo(dimTextProj).DotProduct(upVector); Point extP1 = _Tg.CreatePoint( point1.X + upVector.X * dotP1, point1.Y + upVector.Y * dotP1, point1.Z + upVector.Z * dotP1); Point extP2 = _Tg.CreatePoint( point2.X + upVector.X * dotP2, point2.Y + upVector.Y * dotP2, point2.Z + upVector.Z * dotP2); double dimValue = extP1.DistanceTo(extP2); GraphicsNode node = _clientGraphicsMng.CreateNewGraphicsNode(); LineGraphics extLine1 = _clientGraphicsMng.DrawLine( AdnInventorUtilities.ToArray(point1), AdnInventorUtilities.ToArray(extP1), node); LineGraphics extLine2 = _clientGraphicsMng.DrawLine( AdnInventorUtilities.ToArray(point2), AdnInventorUtilities.ToArray(extP2), node); LineGraphics dimLine = _clientGraphicsMng.DrawLine( AdnInventorUtilities.ToArray(extP1), AdnInventorUtilities.ToArray(extP2), node); extLine1.LineType = LineTypeEnum.kDashedLineType; extLine2.LineType = LineTypeEnum.kDashedLineType; UnitVector v = extP1.VectorTo(extP2).AsUnitVector(); double length = 20.0; double radius = 7.0; Point bottom1 = _Tg.CreatePoint( extP1.X + length * v.X, extP1.Y + length * v.Y, extP1.Z + length * v.Z); Point bottom2 = _Tg.CreatePoint( extP2.X - length * v.X, extP2.Y - length * v.Y, extP2.Z - length * v.Z); SurfaceBody cone1 = _TBrep.CreateSolidCylinderCone( bottom1, extP1, radius, radius, 0.0, null); SurfaceBody cone2 = _TBrep.CreateSolidCylinderCone( bottom2, extP2, radius, radius, 0.0, null); GraphicsNode dimNode = _clientGraphicsMng.CreateNewGraphicsNode(); SurfaceGraphics arrow1 = _clientGraphicsMng.DrawSurface(cone1, dimNode); SurfaceGraphics arrow2 = _clientGraphicsMng.DrawSurface(cone2, dimNode); arrow1.SetTransformBehavior(extP1, DisplayTransformBehaviorEnum.kPixelScaling, 1.0); arrow2.SetTransformBehavior(extP2, DisplayTransformBehaviorEnum.kPixelScaling, 1.0); TextGraphics text = _clientGraphicsMng.DrawText( AdnInventorUtilities.GetStringFromAPILength(dimValue), false, dimNode); text.Font = "Arial"; text.Bold = false; text.Italic = false; text.FontSize = 20; text.PutTextColor(221, 0, 0); text.VerticalAlignment = VerticalTextAlignmentEnum.kAlignTextMiddle; text.HorizontalAlignment = HorizontalTextAlignmentEnum.kAlignTextLeft; Point txtPos = _Tg.CreatePoint( (extP1.X + extP2.X) * 0.5, (extP1.Y + extP2.Y) * 0.5, (extP1.Z + extP2.Z) * 0.5); text.Anchor = txtPos; text.SetTransformBehavior(txtPos, DisplayTransformBehaviorEnum.kFrontFacingAndPixelScaling, 1.0); node.Selectable = true; dimNode.Selectable = true; return(new DimData(point1, point2, extLine1, extLine2, dimLine, dimNode)); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // ClientGraphics simulation running within InteractionEvents using overlay or preview graphics // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void RunWithInteraction(bool overlay) { Initialize(); System.Windows.Forms.Application.AddMessageFilter(new MsgFilter()); Inventor.Application InvApp = AdnInventorUtilities.InvApplication; AssemblyDocument doc = InvApp.ActiveDocument as AssemblyDocument; AssemblyComponentDefinition compDef = doc.ComponentDefinition; Inventor.View view = InvApp.ActiveView; Matrix matrix = InvApp.TransientGeometry.CreateMatrix(); AdnInteractionManager interactionManager = new AdnInteractionManager(InvApp); interactionManager.Initialize(); AdnClientGraphicsManager clientGraphicsMng = new AdnClientGraphicsManager( InvApp, AdnInventorUtilities.AddInGuid); clientGraphicsMng.InteractionGraphicsMode = (overlay ? AdnInteractionGraphicsModeEnum.kOverlayGraphics : AdnInteractionGraphicsModeEnum.kPreviewGraphics); foreach (ComponentOccurrence occurrence in compDef.Occurrences) { occurrence.Visible = false; } interactionManager.Start("Simulation with InteractionEvents"); clientGraphicsMng.SetGraphicsSource(interactionManager.InteractionEvents); List <GraphicsNode> nodes = new List <GraphicsNode>(); foreach (ComponentOccurrence occurrence in compDef.Occurrences) { SurfaceBody body = occurrence.Definition.SurfaceBodies[1]; SurfaceGraphics surfGraph = clientGraphicsMng.DrawSurface(body, null); GraphicsNode node = surfGraph.Parent; if (occurrence.RenderStyle != null) { node.RenderStyle = occurrence.RenderStyle; } nodes.Add(node); } AdnTimer timer = new AdnTimer(); double[] transfo = new double[16]; _bActive = true; while (_bActive) { System.Windows.Forms.Application.DoEvents(); double dT = timer.ElapsedSeconds; _dynamicsWorld.StepSimulation(dT, 10); int idx = 0; foreach (RigidBody body in _mapOccurrencesToBodies.Values) { body.GetWorldTransform(ref transfo); matrix.PutMatrixData(ref transfo); nodes[idx].Transformation = matrix; ++idx; } clientGraphicsMng.UpdateView(); ComputeFrameRate(dT); } _dynamicsWorld.CleanUp(); interactionManager.Stop(); foreach (ComponentOccurrence occurrence in compDef.Occurrences) { occurrence.Visible = true; } }