ComputerGraphics/20210129/zzx-20210129作业/GraphicsStudyCmd.cs

149 lines
5.7 KiB
C#
Raw Normal View History

2021-02-25 15:53:31 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.IO;
using System.Windows;
namespace GraphicsStudy
{
[Transaction(TransactionMode.Manual)]
public class GraphicsStudyCmd : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
string path = @"D:\phpstudy_pro\WWW\demo\res\ceshi22.json";
StreamWriter sw = new StreamWriter(path);
List<Reference> selRefList;
//try
//{
// selRefList = uidoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element).ToList();
//}
//catch
//{
// return Result.Succeeded;
//}
//FamilyInstance fi = doc.GetElement(selRefList[0]) as FamilyInstance;
//BeamData bd = new BeamData(doc, doc.ActiveView.GenLevel, 3500.ToFeet(), (fi.Location as LocationCurve).Curve as Line, fi.Symbol);
//bd.Width = 100;
//bd.Heigth = 500;
//bd.Number = "D1111";
Transaction trans = new Transaction(doc, "123");
trans.Start();
string ddd = "1E88AB71-D7B1-4BE4-A531-09DBE9F62A8E";
//bool b = doc.SetData<string>(ddd, "ceshi", bd.ToJson());
//doc.DeleteData(ddd);
string b = doc.GetData<string>(ddd, "ceshi");
string[] lineArray = b.Split(new string[] { "\r\n" }, StringSplitOptions.None);
BeamData nwBD = new BeamData();
nwBD.GetJson(lineArray);
trans.Commit();
MessageBox.Show(b.ToString());
MessageBox.Show(nwBD.Number);
return Result.Succeeded;
List<Instance> instanceList = new List<Instance>();
List<MeshData> mdList = new List<MeshData>();
MaterialData materData = new MaterialData();
for (int ii = 0; ii < selRefList.Count; ii++)
{
Reference selRef = selRefList[ii];
Element selElem = doc.GetElement(selRef);
//设置instance
Instance ins = new Instance();
ins.Id = $"\"{selElem.UniqueId}\"";
ins.Mesh = ii;
ins.Visible = 1.0;
ins.Transform = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
ins.Info = new List<string>() { $"\n\"\":\"{selElem.Name}\"" };
instanceList.Add(ins);
//设置mesh
var geo = selElem.get_Geometry(new Options());
List<Solid> solidList = GeoMethod.GetSolids(geo);
MeshData md = new MeshData();
foreach (var s in solidList)
{
foreach (var solidFace in s.Faces)
{
PlanarFace planarFace = solidFace as PlanarFace;
ElementId materId = planarFace.MaterialElementId;
if (materId != ElementId.InvalidElementId&&materId!=null)
{
Material faceMaterial = doc.GetElement(materId) as Material;
md.MaterialIndex= materData.AddColor(faceMaterial.Color, faceMaterial.Transparency);
}
//得到面的网格
Mesh faceMesh = planarFace.Triangulate();
//得到面的顶点在总的顶点中的索引
List<int> meshVIndexList = md.AddVertice(faceMesh.Vertices, planarFace.FaceNormal);
//遍历三角面片
for (int i = 0; i < faceMesh.NumTriangles; i++)
{
MeshTriangle mt = faceMesh.get_Triangle(i);
for (int j = 0; j < 3; j++)
{
int index = (int)mt.get_Index(j);
md.IndiceList.Add(meshVIndexList[index]);
}
}
}
}
md.MaterialIndex = ii;
mdList.Add(md);
}
//输出数据
sw.Write("{\n");
sw.Write("\"instances\":[");
instanceList.ForEach(x => sw.Write(x.ToJson()));
sw.Write("\n],");
sw.Write("\"meshes\":[");
mdList.ForEach(md => sw.Write(md.ToJosn()));
sw.Write("\n],");
sw.Write("\"materials\":[");
sw.Write(materData.ToJosn());
sw.Write("\n]");
sw.Write("\n}");
sw.Close();
MessageBox.Show("生成成功");
#region
//Instance i = new Instance();
//i.Id = 12345;
//i.Mesh = 0;
//i.Info = 1;
//i.Transform = new int[]{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
//i.Visible = 0.5;
//string iToJosn = i.ToJson();
//string path = @"C:\Users\UBIMC-104\Desktop\josn导出\ceshi.json";
//StreamWriter sw = new StreamWriter(path);
//sw.Write("\"instances\":[");
//sw.Write(iToJosn);
//sw.Write("\r\n],");
//sw.Close();
#endregion
return Result.Succeeded;
}
}
}