ComputerGraphics/20210129/ljs/LjsGo.Navisworks/GraphicsAssignment/ExportGeometryToJson/ExportGeometryToJsonCmd.cs

135 lines
5.2 KiB
C#
Raw Normal View History

2021-02-20 15:27:11 +08:00
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<Instance>();
var meshs = new List<Mesh>();
var materials = new List<Material> { 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<string>();
var normals_str = new List<string>();
var vertices = new List<double>();
var normals = new List<double>();
var indices = new List<int>();
foreach (InwOaFragment3 frag in path.Fragments())
{
InwLTransform3f3 localToWorld = (InwLTransform3f3)(object)frag.GetLocalToWorldMatrix();
callbkListener.LCS2WCS = localToWorld;
var transfrom = new List<double>();
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;
}
}
}