void DoDemo() { Inventor.Application InvApp = AdnInventorUtilities.InvApplication; //Create new interaction event _interactionEvents = InvApp.CommandManager.CreateInteractionEvents(); //Store mouse events _mouseEvents = _interactionEvents.MouseEvents; //Enable mouse move. This is disabled by default for performance reasons _mouseEvents.MouseMoveEnabled = true; //Listen to OnMouseMove event _mouseEvents.OnMouseMove += new MouseEventsSink_OnMouseMoveEventHandler(MouseEvents_OnMouseMove); _interactionEvents.StatusBarText = "Select triangle vertex: "; //Retrieve InteractionGraphics InteractionGraphics ig = _interactionEvents.InteractionGraphics; //Create new node GraphicsNode node = ig.OverlayClientGraphics.AddNode(1); //Add Triangle primitive _triangleGraph = node.AddTriangleGraphics(); //Set up coordinates GraphicsCoordinateSet coordSet = ig.GraphicsDataSets.CreateCoordinateSet(1); double[] coords = new double[] { 0.0, 0.0, 0.0, //vertex 1 5.0, 0.0, 0.0, //vertex 2 2.5, 5.0, 0.0 //vertex 3 }; coordSet.PutCoordinates(ref coords); _triangleGraph.CoordinateSet = coordSet; _interactionEvents.Start(); }
public TriangleGraphics DrawTriangle( double[] v1, double[] v2, double[] v3, GraphicsNode node) { try { AdnGraphics graphicsData = WorkingGraphics; if (node == null) { node = graphicsData.ClientGraphics.AddNode( graphicsData.GetGraphicNodeFreeId()); } TriangleGraphics graphic = node.AddTriangleGraphics(); if ((v1 != null) && (v2 != null) && (v3 != null)) { GraphicsCoordinateSet coordSet = graphicsData.GraphicsDataSets.CreateCoordinateSet( graphicsData.GetDataSetFreeId()); List <double> coordinates = new List <double>(); coordinates.AddRange(v1); coordinates.AddRange(v2); coordinates.AddRange(v3); double[] coordsArray = coordinates.ToArray(); coordSet.PutCoordinates(ref coordsArray); graphic.CoordinateSet = coordSet; } return(graphic); } catch { return(null); } }
public TriangleGraphics DrawTriangle( double[] v1, double[] v2, double[] v3, GraphicsNode node) { try { AdnGraphics graphicsData = WorkingGraphics; if (node == null) { node = graphicsData.ClientGraphics.AddNode( graphicsData.GetGraphicNodeFreeId()); } TriangleGraphics graphic = node.AddTriangleGraphics(); if ((v1 != null) && (v2 != null) && (v3 != null)) { GraphicsCoordinateSet coordSet = graphicsData.GraphicsDataSets.CreateCoordinateSet( graphicsData.GetDataSetFreeId()); List<double> coordinates = new List<double>(); coordinates.AddRange(v1); coordinates.AddRange(v2); coordinates.AddRange(v3); double[] coordsArray = coordinates.ToArray(); coordSet.PutCoordinates(ref coordsArray); graphic.CoordinateSet = coordSet; } return graphic; } catch { return null; } }
////////////////////////////////////////////////////////////////////////////////////////////// // Simple example using Inventor API directly // ////////////////////////////////////////////////////////////////////////////////////////////// public static void ClientFeatureDemo() { string clientId = AdnInventorUtilities.AddInGuid; Inventor.Application InvApp = AdnInventorUtilities.InvApplication; Document document = InvApp.ActiveDocument; // We will use late binding to retrieve ClientFeatures collection, // so we dont need to write specific code for PartDocument and // AssemblyDocument ComponentDefinition compDef = AdnInventorUtilities.GetCompDefinition(document); object features = AdnInventorUtilities.GetProperty(compDef, "Features"); ClientFeatures clientFeatures = AdnInventorUtilities.GetProperty(features, "ClientFeatures") as ClientFeatures; ClientFeatureDefinition cfDef = clientFeatures.CreateDefinition("Graphics Feature", null, null, null); ClientFeature clientFeature = clientFeatures.Add(cfDef, clientId); NativeBrowserNodeDefinition nodeDef = clientFeature.BrowserNode.BrowserNodeDefinition as NativeBrowserNodeDefinition; stdole.IPictureDisp pic = PictureDispConverter.ToIPictureDisp(Resources.PointImage); ClientNodeResource res = document.BrowserPanes.ClientNodeResources.Add( clientId, document.BrowserPanes.ClientNodeResources.Count + 1, pic); nodeDef.OverrideIcon = res; cfDef = clientFeature.Definition; cfDef.HighlightClientGraphicsWithFeature = true; GraphicsDataSets sets = cfDef.GraphicsDataSetsCollection.Add2(clientId, true); GraphicsCoordinateSet coordSet = sets.CreateCoordinateSet(1); double[] coords = new double[] { 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 2.5, 5.0, 0.0 }; coordSet.PutCoordinates(ref coords); ClientGraphics cg = cfDef.ClientGraphicsCollection.Add(clientId); GraphicsNode node = cg.AddNode(1); node.RenderStyle = document.RenderStyles["Green (Flat)"]; TriangleGraphics primitive = node.AddTriangleGraphics(); primitive.CoordinateSet = coordSet; InvApp.ActiveView.Update(); }
/// <summary> /// Simplifies the mesh provided, then places it back into the parameters. /// </summary> /// <remarks> /// Repeatedly remove the shortest edge in the mesh, reducing the triangle count by two each step. This is repeated /// until the triangle count is low enough or more than 2000 tries have been made. /// </remarks> /// <param name="verts">The original and final verticies. (3 elements per vertex)</param> /// <param name="vertCount">The original and final vertex counts.</param> /// <param name="inds">The original and final index buffer. (3 element per triangle, zero based)</param> /// <param name="trisCount">The original and final triangle counts.</param> private static void Simplify(ref float[] verts, ref uint vertCount, ref uint[] inds, ref uint trisCount) { // Setup edge relations, compute min edge lengths... List <SimplificationVertex> simplVerts = new List <SimplificationVertex>(); for (int i = 0; i < vertCount; i++) { simplVerts.Add(new SimplificationVertex(verts[i * 3], verts[i * 3 + 1], verts[i * 3 + 2])); } List <SimplificationFace> simplFace = new List <SimplificationFace>(); for (int i = 0; i < trisCount * 3; i += 3) { SimplificationFace face = new SimplificationFace(); face.verts[0] = simplVerts[(int)inds[i]]; face.verts[1] = simplVerts[(int)inds[i + 1]]; face.verts[2] = simplVerts[(int)inds[i + 2]]; foreach (SimplificationVertex v in face.verts) { v.faces.Add(face); // Make sure all verticies know their neighbors } simplFace.Add(face); } foreach (SimplificationFace face in simplFace) { face.updateInfo(); } simplFace.Sort(); #region FANCY_SIMPLIFY_GRAPHICS #if FANCY_SIMPLIFY_GRAPHICS double[] major, minor; GraphicsCoordinateSet majorCoordSet, minorCoordSet; major = new double[9 * simplFace.Count]; for (int a = 0; a < simplFace.Count; a++) { int off = a * 9; simplFace[a].tmpHead = off; Array.Copy(simplFace[a].verts[0].pos, 0, major, off, 3); Array.Copy(simplFace[a].verts[1].pos, 0, major, off + 3, 3); Array.Copy(simplFace[a].verts[2].pos, 0, major, off + 6, 3); } minor = new double[9]; AssemblyDocument part = ((AssemblyDocument)Exporter.INVENTOR_APPLICATION.ActiveDocument); GraphicsDataSets dataSets; try { dataSets = part.GraphicsDataSetsCollection.AddNonTransacting("convexHULL"); } catch { dataSets = part.GraphicsDataSetsCollection["convexHULL"]; dataSets.Delete(); dataSets = part.GraphicsDataSetsCollection.AddNonTransacting("convexHULL"); } ClientGraphics graphics; try { graphics = part.ComponentDefinition.ClientGraphicsCollection.AddNonTransacting("convexHULL"); } catch { graphics = part.ComponentDefinition.ClientGraphicsCollection["convexHULL"]; graphics.Delete(); graphics = part.ComponentDefinition.ClientGraphicsCollection.AddNonTransacting("convexHULL"); } { GraphicsNode node = graphics.AddNode(graphics.Count + 1); majorCoordSet = dataSets.CreateCoordinateSet(dataSets.Count + 1); majorCoordSet.PutCoordinates(major); LineGraphics primitive = node.AddLineGraphics(); primitive.BurnThrough = false; //Create Coordinate Index Set GraphicsIndexSet indexSetCoords = dataSets.CreateIndexSet(dataSets.Count + 1); for (int i = 0; i < major.Length; i += 3) { indexSetCoords.Add(indexSetCoords.Count + 1, i + 1); //from point 1 indexSetCoords.Add(indexSetCoords.Count + 1, i + 2); //connect to point 2 indexSetCoords.Add(indexSetCoords.Count + 1, i + 2); //from point 2 indexSetCoords.Add(indexSetCoords.Count + 1, i + 3); //connect to point 3 indexSetCoords.Add(indexSetCoords.Count + 1, i + 3); //from point 3 indexSetCoords.Add(indexSetCoords.Count + 1, i + 1); //connect to point 1 } primitive.CoordinateSet = majorCoordSet; primitive.CoordinateIndexSet = indexSetCoords; } { GraphicsNode node = graphics.AddNode(graphics.Count + 1); minorCoordSet = dataSets.CreateCoordinateSet(dataSets.Count + 1); minorCoordSet.PutCoordinates(minor); TriangleGraphics primitive = node.AddTriangleGraphics(); primitive.DepthPriority = 19999; //Create Coordinate Index Set GraphicsColorSet colorSet = dataSets.CreateColorSet(dataSets.Count + 1); for (int i = 0; i < minor.Length; i += 3) { colorSet.Add(colorSet.Count + 1, 255, 0, 0); } primitive.CoordinateSet = minorCoordSet; primitive.ColorSet = colorSet; primitive.ColorBinding = ColorBindingEnum.kPerItemColors; //primitive.CoordinateIndexSet = indexSetCoords; } Exporter.INVENTOR_APPLICATION.ActiveView.Update(); #endif #endregion // Time for shenanigans! We are going to naively pick the shortest edge and then remove it. Destroys two faces per loop typically. // Give it 2000 tries at most. for (int i = 0; i < 2000 && simplFace.Count > CONVEX_HULL_FACET_LIMIT; i++) { SimplificationFace bFace = simplFace[0]; // This is the edge to remove SimplificationVertex[] remove = new SimplificationVertex[] { bFace.verts[bFace.minEdge], bFace.verts[(bFace.minEdge + 1) % bFace.verts.Length] }; #region FANCY_SIMPLIFY_GRAPHICS #if FANCY_SIMPLIFY_GRAPHICS // Highlight Array.Copy(bFace.verts[0].pos, 0, minor, 0, 3); Array.Copy(bFace.verts[1].pos, 0, minor, 3, 3); Array.Copy(bFace.verts[2].pos, 0, minor, 6, 3); minorCoordSet.PutCoordinates(minor); Exporter.INVENTOR_APPLICATION.ActiveView.Update(); #endif #endregion // Find the center point of the edge. One edge -> one vertex float[] center = new float[3]; foreach (SimplificationVertex vert in remove) { center[0] += vert.pos[0] / 2.0f; center[1] += vert.pos[1] / 2.0f; center[2] += vert.pos[2] / 2.0f; vert.faces.Clear(); // Really, never use vertex again. } SimplificationVertex newVertex = new SimplificationVertex(center[0], center[1], center[2]); // Ineffeciently check each face to see if it shares a vertex with the edge for (int k = simplFace.Count - 1; k >= 0; k--) { int matched = 0; SimplificationFace face = simplFace[k]; for (int j = 0; j < face.verts.Length; j++) { if (face.verts[j] == remove[0] || face.verts[j] == remove[1]) { face.verts[j] = newVertex; #region FANCY_SIMPLIFY_GRAPHICS #if FANCY_SIMPLIFY_GRAPHICS Array.Copy(face.verts[j].pos, 0, major, face.tmpHead + (3 * j), 3); int index = (face.tmpHead / 3) + j + 1; majorCoordSet.Remove(index); majorCoordSet.Add(index, Exporter.INVENTOR_APPLICATION.TransientGeometry.CreatePoint(face.verts[j].pos[0], face.verts[j].pos[1], face.verts[j].pos[2])); #endif #endregion if (matched == 0) { newVertex.faces.Add(face); } matched++; } } // If we share at least two verts with the edge we are dead, since the triangle ends up with two // of the same vertex if (matched >= 2) { // Degenerate simplFace.RemoveAt(k); foreach (SimplificationVertex v in face.verts) { v.faces.Remove(face); } } else if (matched == 1) { // We changed, so update edge lengths face.updateInfo(); } } simplVerts.Add(newVertex); // Resort by edge length simplFace.Sort(); } simplVerts.RemoveAll((vert) => vert.faces.Count <= 0); // Rebuild arrays vertCount = (uint)simplVerts.Count; verts = new float[vertCount * 3]; for (int i = 0; i < simplVerts.Count; i++) { int off = i * 3; simplVerts[i].finalIndex = (i); // Our indices are zero based <3 Array.Copy(simplVerts[i].pos, 0, verts, off, 3); } trisCount = (uint)simplFace.Count; inds = new uint[trisCount * 3]; for (int i = 0; i < simplFace.Count; i++) { int off = i * 3; inds[off] = (uint)simplFace[i].verts[0].finalIndex; inds[off + 1] = (uint)simplFace[i].verts[1].finalIndex; inds[off + 2] = (uint)simplFace[i].verts[2].finalIndex; } }
public void CreateMesh(NameValueMap nv) { var N = 512; var T0 = DateTime.Now; if (null != mGraphicsDataSets) { mGraphicsDataSets.Delete(); mGraphicsDataSets = null; mGraphicsNode.Delete(); mGraphicsNode = null; mApp.ActiveView.Update(); return; } var doc = mApp.ActiveDocument as PartDocument; if (null == mClientGraphics) { mClientGraphics = doc.ComponentDefinition.ClientGraphicsCollection.Add("MyTest"); } mGraphicsDataSets = doc.GraphicsDataSetsCollection.Add("MyTest"); var setupTimeSeconds = (DateTime.Now - T0).TotalSeconds; T0 = DateTime.Now; var msg = string.Format("N = {0}, {1} triangles\n", N, mNumTriangles) + string.Format("Inventor setup time: {0} sec\n", setupTimeSeconds); T0 = DateTime.Now; try { var dataSetsTimeSeconds = (DateTime.Now - T0).TotalSeconds; T0 = DateTime.Now; var gcs = mGraphicsDataSets.CreateCoordinateSet(1) as GraphicsCoordinateSet; gcs.PutCoordinates(mVertices); var coordSetSeconds = (DateTime.Now - T0).TotalSeconds; T0 = DateTime.Now; var gis = mGraphicsDataSets.CreateIndexSet(2) as GraphicsIndexSet; gis.PutIndices(mIndices); var indexSetSeconds = (DateTime.Now - T0).TotalSeconds; T0 = DateTime.Now; var gns = mGraphicsDataSets.CreateNormalSet(3) as GraphicsNormalSet; gns.PutNormals(mNormals); var normalSetSeconds = (DateTime.Now - T0).TotalSeconds; T0 = DateTime.Now; mGraphicsNode = mClientGraphics.AddNode(1) as GraphicsNode; var triangles = mGraphicsNode.AddTriangleGraphics() as TriangleGraphics; triangles.CoordinateSet = gcs; triangles.CoordinateIndexSet = gis; triangles.NormalSet = gns; triangles.NormalBinding = NormalBindingEnum.kPerItemNormals; triangles.NormalIndexSet = gis; var trianglesSeconds = (DateTime.Now - T0).TotalSeconds; bool inActiveDocument; var brassAsset = GetAsset("Brass - Satin", out inActiveDocument); if (null != brassAsset) { if (!inActiveDocument) { brassAsset = brassAsset.CopyTo(doc); } mGraphicsNode.Appearance = brassAsset; } msg = msg + string.Format("GraphicsDataSetsCollection.Add time: {0} sec\n", dataSetsTimeSeconds) + string.Format("Coordinate set creation time: {0} sec\n", coordSetSeconds) + string.Format("Index set creation time: {0} sec\n", indexSetSeconds) + string.Format("Normal set creation time: {0} sec\n", normalSetSeconds) + string.Format("Triangle node creation time: {0} sec\n", trianglesSeconds); } catch (Exception e) { msg += string.Format("Exception: {0}", e.ToString()); } mApp.ActiveView.Update(); System.Windows.MessageBox.Show(msg); }
public void Draw3D(Part Bauteil, string Name) { foreach (var mmface in Bauteil.Faces) { foreach (var point in mmface.VertexCoords) { mVertices.Add((double)point.X); mVertices.Add((double)point.Y); mVertices.Add((double)point.Z); } var N = mmface.VertexIndices.Count; // Nomber of Indices foreach (var indice in mmface.VertexIndices) { var i0 = indice; var i1 = i0 + N; var t0 = i0; var t1 = i1; // NOTE: The extra +1 on each line is because Inventor expects 1 - based indices. mIndices.Add(t1 + 1 + 1); mIndices.Add(t0 + 1); mIndices.Add(t0 + 1 + 1); // mIndices.Add(t1 + 1 + 1); // mIndices.Add(t0 + 1); mIndices.Add(t1 + 1); } foreach (var Norm in mmface.Normals) { mNormals.Add((double)Norm.X); mNormals.Add((double)Norm.Y); mNormals.Add((double)Norm.Z); } } if (null != mGraphicsDataSets) { mGraphicsDataSets.Delete(); mGraphicsDataSets = null; mGraphicsNode.Delete(); mGraphicsNode = null; Addin3DPdf.TrAddInServer.MApp.ActiveView.Update(); return; } PartDocument doc = Addin3DPdf.TrAddInServer.MApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, Addin3DPdf.TrAddInServer.MApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject), true) as PartDocument; // PartDocument doc = Addin3DPdf.TransAddInServer.MApp.ActiveDocument as PartDocument; if (null == mClientGraphics) { mClientGraphics = doc.ComponentDefinition.ClientGraphicsCollection.Add(Name); } mGraphicsDataSets = doc.GraphicsDataSetsCollection.Add(Name); double[] A_Verties = mVertices.ToArray(); int[] A_Indices = mIndices.ToArray(); double[] A_Normals = mNormals.ToArray(); try { var gcs = mGraphicsDataSets.CreateCoordinateSet(1) as GraphicsCoordinateSet; gcs.PutCoordinates(A_Verties); var gis = mGraphicsDataSets.CreateIndexSet(2) as GraphicsIndexSet; gis.PutIndices(A_Indices); var gns = mGraphicsDataSets.CreateNormalSet(3) as GraphicsNormalSet; gns.PutNormals(A_Normals); mGraphicsNode = mClientGraphics.AddNode(1) as GraphicsNode; var triangles = mGraphicsNode.AddTriangleGraphics() as TriangleGraphics; triangles.CoordinateSet = gcs; triangles.CoordinateIndexSet = gis; triangles.NormalSet = gns; triangles.NormalBinding = NormalBindingEnum.kPerItemNormals; triangles.NormalIndexSet = gis; bool inActiveDocument; var brassAsset = GetAsset("Silver", out inActiveDocument); if (null != brassAsset) { if (!inActiveDocument) { brassAsset = brassAsset.CopyTo(doc); } mGraphicsNode.Appearance = brassAsset; } } catch (Exception e) { } doc.SaveAs(Name, false); Addin3DPdf.TrAddInServer.MApp.ActiveView.Update(); }