using Autodesk.Navisworks.Api; using Autodesk.Navisworks.Api.Plugins; using System; using System.Collections.Generic; using System.Text; using System.Windows; using Application = Autodesk.Navisworks.Api.Application; using ComApi = Autodesk.Navisworks.Api.Interop.ComApi; using LjsGo.Navisworks.GraphicsAssignment.ExportGeometryToJson.Models; using System.IO; using System.Reflection; using Autodesk.Navisworks.Api.ComApi; using LjsGo.Navisworks.GraphicsAssignment.ExportGeometryToJson; using Autodesk.Navisworks.Api.Interop.ComApi; namespace LjsGo.Navisworks.GraphicsAssignment { //基本插件 [Plugin("Basic", "ADSK", ToolTip = "Popups", DisplayName = "TestTest")]//名字,开发者号(四位字串),提示,用户界面显示名 [AddInPlugin(AddInLocation.AddIn)]//设定插件的位置 public class ExportGeometryToJsonCmd : AddInPlugin { private static string AssemblyPath = Path.GetDirectoryName(typeof(ExportGeometryToJsonCmd).Assembly.Location); public override int Execute(params string[] parameters) { try { //当前文档 Document doc = Application.ActiveDocument;//application是运行了之后自动提供的 // get the current selection ModelItemCollection oModelColl = Application.ActiveDocument.CurrentSelection.SelectedItems; //convert to COM selection InwOpState oState = ComApiBridge.State; InwOpSelection oSel = ComApiBridge.ToInwOpSelection(oModelColl); // create the callback object var instances = new List(); var meshs = new List(); var materials = new List { new Material { color = "#808080", transparent = 1.0 }}; ModelData modelData = new ModelData { instances = instances, meshes = meshs, materials = materials, }; var instanceIndex = 0; foreach (InwOaPath3 path in oSel.Paths()) { Instance instance = new Instance { id = Guid.NewGuid().ToString(), info = new { Name = "test" }, mesh = instanceIndex, visible = 1, }; instances.Add(instance); CallbackGeomListener callbkListener = new CallbackGeomListener(); var vertices_str = new List(); var normals_str = new List(); var vertices = new List(); var normals = new List(); var indices = new List(); foreach (InwOaFragment3 frag in path.Fragments()) { InwLTransform3f3 localToWorld = (InwLTransform3f3)(object)frag.GetLocalToWorldMatrix(); callbkListener.LCS2WCS = localToWorld; var transfrom = new List(); foreach (double item in (Array)localToWorld.Matrix) { transfrom.Add(item); } instance.transform = transfrom.ToArray(); // generate the primitives frag.GenerateSimplePrimitives(nwEVertexProperty.eNORMAL, callbkListener); vertices_str.AddRange(callbkListener.vertexes_str); normals_str.AddRange(callbkListener.normals_str); vertices.AddRange(callbkListener.vertexes); normals.AddRange(callbkListener.normals); for (int q = 0; q < callbkListener.vertexes_str.Count && q < callbkListener.normals_str.Count; q++) { for (int i = 0; i < vertices_str.Count && i < normals_str.Count; i++) { if (vertices_str[i] == callbkListener.vertexes_str[q] && normals_str[i] == callbkListener.normals_str[q]) { indices.Add(i); break; } } } } Mesh mesh = new Mesh() { vertices = vertices, normals = normals, indices = indices, material = 0, }; meshs.Add(mesh); instanceIndex++; } var json = Kooboo.Json.JsonSerializer.ToJson(modelData); var fileInfo = new FileInfo(doc.FileName); var dir = fileInfo.Directory.FullName; var savePath = Path.Combine(dir, "test.json"); File.WriteAllText(savePath, json); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return 0; } } }