Files
SZ-IFC/IFC_Revit_Interop_VERSION_2018/IFC_Revit_Interop/Rvt2Obj.cs
2026-02-23 16:55:57 +08:00

568 lines
16 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Visual;
using IFC_Revit_Interop.Utils;
using Microsoft.Win32;
namespace IFC_Revit_Interop
{
// Token: 0x0200000A RID: 10
internal class Rvt2Obj : IExportContext
{
// Token: 0x0600002A RID: 42 RVA: 0x00003B30 File Offset: 0x00001D30
public Rvt2Obj(Document doc, string objSavePath, string textureDir, int meshLod, Log log = null)
{
this.resultPath = objSavePath;
this.resultTexturePath = textureDir;
if (!Directory.Exists(this.resultTexturePath))
{
Directory.CreateDirectory(this.resultTexturePath);
}
this.m_doc = doc;
this.m_TransformationStack.Push(Transform.Identity);
this._precision = meshLod;
this.log = log;
}
// Token: 0x0600002B RID: 43 RVA: 0x00003BD4 File Offset: 0x00001DD4
public bool Start()
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(this.resultPath);
this.swObj = new StreamWriter(Path.GetDirectoryName(this.resultPath) + "\\" + fileNameWithoutExtension + ".obj");
this.swObj.Write("mtllib " + fileNameWithoutExtension + ".mtl\n");
this.swMtl = new StreamWriter(Path.GetDirectoryName(this.resultPath) + "\\" + fileNameWithoutExtension + ".mtl");
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey registryKey = localMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Autodesk\\ADSKTextureLibrary\\1");
if (registryKey != null && int.Parse("2018") < 2020)
{
this.textureLibraryPath = registryKey.GetValue("LibraryPaths").ToString();
}
else
{
registryKey = localMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Autodesk\\ADSKTextureLibraryNew\\1");
if (registryKey != null)
{
string[] subKeyNames = registryKey.GetSubKeyNames();
if (subKeyNames.Length != 0)
{
registryKey = localMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Autodesk\\ADSKTextureLibraryNew\\1\\" + subKeyNames[0]);
}
if (registryKey != null)
{
this.textureLibraryPath = registryKey.GetValue("LibraryPaths").ToString();
}
}
}
if (string.IsNullOrEmpty(this.textureLibraryPath))
{
Log log = this.log;
if (log != null)
{
log.Write("Texture", "Revit Texture Library not found in Regedit.");
}
return false;
}
localMachine.Close();
registryKey.Close();
return true;
}
// Token: 0x0600002C RID: 44 RVA: 0x00003D10 File Offset: 0x00001F10
public RenderNodeAction OnViewBegin(ViewNode node)
{
node.LevelOfDetail = this._precision;
return 0;
}
// Token: 0x0600002D RID: 45 RVA: 0x00003D1F File Offset: 0x00001F1F
public RenderNodeAction OnLinkBegin(LinkNode node)
{
return (RenderNodeAction)1;
}
// Token: 0x0600002E RID: 46 RVA: 0x00003D24 File Offset: 0x00001F24
public RenderNodeAction OnElementBegin(ElementId elementId)
{
this.currentId = elementId;
this.elementPolyIndex = 0;
this.productTris[this.currentId.IntegerValue] = 0;
Utils_SendMessage.UpdateProgressUI(5, 90, (double)this.currentElementCount * 1.0 / (double)this.totalElementCount);
this.currentElementCount++;
return 0;
}
// Token: 0x0600002F RID: 47 RVA: 0x00003D88 File Offset: 0x00001F88
private Material GetElementTypeMaterial(ElementId eleId)
{
ElementType elementType = this.m_doc.GetElement(eleId) as ElementType;
if (elementType == null)
{
return null;
}
foreach (Parameter parameter in elementType.GetOrderedParameters())
{
if (parameter.StorageType == (StorageType)4)
{
Definition definition = parameter.Definition;
if (definition.ParameterGroup == (BuiltInParameterGroup)(-5000105) && definition.ParameterType == (ParameterType)9)
{
ElementId elementId = parameter.AsElementId();
if (-1 != elementId.IntegerValue)
{
return this.m_doc.GetElement(elementId) as Material;
}
break;
}
}
}
return null;
}
// Token: 0x06000030 RID: 48 RVA: 0x00003E38 File Offset: 0x00002038
private string GetMaterialTexturePath(Material m)
{
if (m == null)
{
return null;
}
ElementId appearanceAssetId = m.AppearanceAssetId;
if (appearanceAssetId != ElementId.InvalidElementId)
{
AppearanceAssetElement appearanceAssetElement = m.Document.GetElement(appearanceAssetId) as AppearanceAssetElement;
if (appearanceAssetElement != null)
{
Asset renderingAsset = appearanceAssetElement.GetRenderingAsset();
if (renderingAsset.Size == 0)
{
using (var enumerator = m.Document.Application.get_Assets((AssetType)4).ForwardIterator())
{
while (enumerator.MoveNext())
{
object obj = enumerator.Current;
Asset asset = (Asset)obj;
if (asset.Name == renderingAsset.Name && asset.LibraryName == renderingAsset.LibraryName)
{
return this.ParseAssetProperty(asset);
}
}
goto IL_00C3;
}
}
return this.ParseAssetProperty(renderingAsset);
}
}
IL_00C3:
return null;
}
// Token: 0x06000031 RID: 49 RVA: 0x00003F1C File Offset: 0x0000211C
public RenderNodeAction OnInstanceBegin(InstanceNode node)
{
this.m_TransformationStack.Push(this.m_TransformationStack.Peek().Multiply(node.GetTransform()));
return 0;
}
// Token: 0x06000032 RID: 50 RVA: 0x00003F40 File Offset: 0x00002140
public void OnMaterial(MaterialNode node)
{
if (this.currentMaterialId != node.MaterialId)
{
this.currentMaterialId = node.MaterialId;
if (this.materialIds.Contains(this.currentMaterialId))
{
return;
}
this.materialIds.Add(this.currentMaterialId);
this.currentColor = node.Color;
this.currentTransparency = 1.0 - node.Transparency;
this.swMtl.Write("\nnewmtl {0}\nka {1} {2} {3}\nkd {1} {2} {3}\nd {4}\n", new object[]
{
this.currentMaterialId.IntegerValue.ToString(),
((double)this.currentColor.Red / 256.0).ToString(),
((double)this.currentColor.Green / 256.0).ToString(),
((double)this.currentColor.Blue / 256.0).ToString(),
this.currentTransparency
});
if (node.HasOverriddenAppearance)
{
this.currentAsset = node.GetAppearanceOverride();
}
else
{
this.currentAsset = node.GetAppearance();
}
try
{
string text = this.ParseAssetProperty(this.GetTrueAsset(this.currentAsset));
if (text != null)
{
string text2 = Regex.Replace(Path.GetDirectoryName(text), "[^0-9]+", "");
this.textureName = Path.GetFileName(text).Replace(" ", "").Replace("&", "-")
.ToLower();
if (text2 != null && text2.Length > 0)
{
this.textureName = Path.GetFileNameWithoutExtension(this.textureName) + "_" + text2 + Path.GetExtension(this.textureName);
}
string text3 = Path.Combine(this.textureLibraryPath, text.Replace("/", "\\"));
if (File.Exists(text3))
{
this.swMtl.Write("map_Kd ./textures/" + this.textureName + "\n");
File.Copy(text3, Path.Combine(this.resultTexturePath, this.textureName), true);
}
}
}
catch (Exception)
{
}
}
}
// Token: 0x06000033 RID: 51 RVA: 0x0000417C File Offset: 0x0000237C
private Asset GetTrueAsset(Asset currentAsset)
{
if (currentAsset.Size == 0)
{
foreach (object obj in this.m_doc.Application.get_Assets((AssetType)4))
{
Asset asset = (Asset)obj;
if (asset.Name == currentAsset.Name && asset.LibraryName == currentAsset.LibraryName)
{
return asset;
}
}
return null;
}
return currentAsset;
}
// Token: 0x06000034 RID: 52 RVA: 0x00004210 File Offset: 0x00002410
public void OnLight(LightNode node)
{
}
// Token: 0x06000035 RID: 53 RVA: 0x00004212 File Offset: 0x00002412
public void OnRPC(RPCNode node)
{
}
// Token: 0x06000036 RID: 54 RVA: 0x00004214 File Offset: 0x00002414
public RenderNodeAction OnFaceBegin(FaceNode node)
{
return 0;
}
// Token: 0x06000037 RID: 55 RVA: 0x00004218 File Offset: 0x00002418
public void OnPolymesh(PolymeshTopology node)
{
TextWriter textWriter = this.swObj;
string[] array = new string[5];
array[0] = "g ";
array[1] = this.currentId.IntegerValue.ToString();
array[2] = "-";
int num = 3;
int integerValue = this.elementPolyIndex;
this.elementPolyIndex = integerValue + 1;
array[num] = integerValue.ToString();
array[4] = "\n";
textWriter.Write(string.Concat(array));
this.swObj.Write("usemtl " + this.currentMaterialId.IntegerValue.ToString() + "\n");
Transform currentTransform = this.m_TransformationStack.Peek();
IEnumerable<XYZ> enumerable = (from p in node.GetPoints()
select currentTransform.OfPoint(p)).ToList<XYZ>();
Dictionary<int, int> dictionary = this.productTris;
integerValue = this.currentId.IntegerValue;
dictionary[integerValue] += node.NumberOfPoints;
foreach (XYZ xyz in enumerable)
{
XYZ xyz2 = xyz * Rvt2Obj.feetToMM;
this.swObj.Write(string.Concat(new string[]
{
"v ",
xyz2.X.ToString(),
" ",
xyz2.Y.ToString(),
" ",
xyz2.Z.ToString(),
"\n"
}));
}
foreach (UV uv in node.GetUVs())
{
this.swObj.Write(string.Concat(new string[]
{
"vt ",
(uv.U * 0.3048).ToString(),
" ",
(uv.V * 0.3048).ToString(),
" 0.0\n"
}));
}
foreach (PolymeshFacet polymeshFacet in node.GetFacets())
{
this.swObj.Write(string.Concat(new string[]
{
"f ",
(polymeshFacet.V1 + 1 + this.vIndex).ToString(),
"/",
(polymeshFacet.V1 + 1 + this.vIndex).ToString(),
" ",
(polymeshFacet.V2 + 1 + this.vIndex).ToString(),
"/",
(polymeshFacet.V2 + 1 + this.vIndex).ToString(),
" ",
(polymeshFacet.V3 + 1 + this.vIndex).ToString(),
"/",
(polymeshFacet.V3 + 1 + this.vIndex).ToString(),
"\n"
}));
}
this.vIndex += node.NumberOfPoints;
}
// Token: 0x06000038 RID: 56 RVA: 0x000045D0 File Offset: 0x000027D0
public void OnFaceEnd(FaceNode node)
{
}
// Token: 0x06000039 RID: 57 RVA: 0x000045D2 File Offset: 0x000027D2
public void OnInstanceEnd(InstanceNode node)
{
this.m_TransformationStack.Pop();
}
// Token: 0x0600003A RID: 58 RVA: 0x000045E0 File Offset: 0x000027E0
public void OnElementEnd(ElementId elementId)
{
}
// Token: 0x0600003B RID: 59 RVA: 0x000045E2 File Offset: 0x000027E2
public void OnLinkEnd(LinkNode node)
{
}
// Token: 0x0600003C RID: 60 RVA: 0x000045E4 File Offset: 0x000027E4
public void OnViewEnd(ElementId elementId)
{
Utils_SendMessage.UpdateProgressUI(95, 95, 1.0);
File.WriteAllLines("D:\\huarun_0105.txt", (from x in this.productTris
orderby x.Value
select string.Format("{0} {1}", x.Key, x.Value)).ToArray<string>());
}
// Token: 0x0600003D RID: 61 RVA: 0x00004660 File Offset: 0x00002860
public bool IsCanceled()
{
return false;
}
// Token: 0x0600003E RID: 62 RVA: 0x00004663 File Offset: 0x00002863
public void Finish()
{
this.swObj.Close();
this.swMtl.Close();
}
// Token: 0x0600003F RID: 63 RVA: 0x0000467C File Offset: 0x0000287C
private bool IsTextureAsset(Asset asset)
{
AssetProperty assetProperty = this.GetAssetProperty(asset, "assettype");
return (assetProperty != null && (assetProperty as AssetPropertyString).Value == "texture") || this.GetAssetProperty(asset, "unifiedbitmap_Bitmap") != null;
}
// Token: 0x06000040 RID: 64 RVA: 0x000046C4 File Offset: 0x000028C4
private AssetProperty GetAssetProperty(Asset asset, string propertyName)
{
for (int i = 0; i < asset.Size; i++)
{
if (asset[i].Name == propertyName)
{
return asset[i];
}
}
return null;
}
// Token: 0x06000041 RID: 65 RVA: 0x00004700 File Offset: 0x00002900
private Asset FindTextureAsset(AssetProperty ap)
{
Asset asset = null;
if (ap.Type == (AssetPropertyType)15)
{
if (!this.IsTextureAsset(ap as Asset))
{
for (int i = 0; i < (ap as Asset).Size; i++)
{
if (this.FindTextureAsset((ap as Asset)[i]) != null)
{
return this.FindTextureAsset((ap as Asset)[i]);
}
}
}
else
{
asset = ap as Asset;
}
return asset;
}
for (int j = 0; j < ap.NumberOfConnectedProperties; j++)
{
if (this.FindTextureAsset(ap.GetConnectedProperty(j)) != null)
{
return this.FindTextureAsset(ap.GetConnectedProperty(j));
}
}
return asset;
}
// Token: 0x06000042 RID: 66 RVA: 0x000047A4 File Offset: 0x000029A4
private string ParseAssetProperty(AssetProperty prop)
{
if (prop == null)
{
return null;
}
if (prop.Name == "unifiedbitmap_Bitmap")
{
string value = (prop as AssetPropertyString).Value;
if (value.Length > 0)
{
return value.Split(new char[] { '|' })[0];
}
}
foreach (AssetProperty assetProperty in prop.GetAllConnectedProperties())
{
string text = this.ParseAssetProperty(assetProperty);
if (text != null)
{
return text;
}
}
AssetPropertyType type = prop.Type;
if (type !=(AssetPropertyType) 1)
{
if (type != (AssetPropertyType)15)
{
if (type == (AssetPropertyType)19)
{
foreach (AssetProperty assetProperty2 in (prop as AssetPropertyList).GetValue())
{
string text2 = this.ParseAssetProperty(assetProperty2);
if (text2 != null)
{
return text2;
}
}
}
}
else
{
Asset asset = prop as Asset;
for (int i = 0; i < asset.Size; i++)
{
AssetProperty assetProperty3 = asset[i];
string text3 = this.ParseAssetProperty(assetProperty3);
if (text3 != null)
{
return text3;
}
}
}
}
else
{
AssetProperties assetProperties = prop as AssetProperties;
for (int j = 0; j < assetProperties.Size; j++)
{
AssetProperty assetProperty4 = assetProperties[j];
string text4 = this.ParseAssetProperty(assetProperty4);
if (text4 != null)
{
return text4;
}
}
}
return null;
}
// Token: 0x04000020 RID: 32
private StreamWriter swObj;
// Token: 0x04000021 RID: 33
private StreamWriter swMtl;
// Token: 0x04000022 RID: 34
private const string strNewmtl = "\nnewmtl {0}\nka {1} {2} {3}\nkd {1} {2} {3}\nd {4}\n";
// Token: 0x04000023 RID: 35
private ElementId currentMaterialId;
// Token: 0x04000024 RID: 36
private Color currentColor;
// Token: 0x04000025 RID: 37
private bool useElementTypeTexture;
// Token: 0x04000026 RID: 38
private HashSet<string> texturePathes = new HashSet<string>();
// Token: 0x04000027 RID: 39
private HashSet<ElementId> materialIds = new HashSet<ElementId>();
// Token: 0x04000028 RID: 40
private double currentTransparency;
// Token: 0x04000029 RID: 41
private Asset currentAsset;
// Token: 0x0400002A RID: 42
private int _precision;
// Token: 0x0400002B RID: 43
private Document m_doc;
// Token: 0x0400002C RID: 44
private Stack<Transform> m_TransformationStack = new Stack<Transform>();
// Token: 0x0400002D RID: 45
private ElementId currentId;
// Token: 0x0400002E RID: 46
private int vIndex;
// Token: 0x0400002F RID: 47
private string textureLibraryPath = "";
// Token: 0x04000030 RID: 48
private string textureName = "";
// Token: 0x04000031 RID: 49
private string resultPath;
// Token: 0x04000032 RID: 50
private string resultTexturePath;
// Token: 0x04000033 RID: 51
private static double feetToMM = 304.8;
// Token: 0x04000034 RID: 52
private int elementPolyIndex;
// Token: 0x04000035 RID: 53
public int totalElementCount;
// Token: 0x04000036 RID: 54
private int currentElementCount;
// Token: 0x04000037 RID: 55
private Log log;
// Token: 0x04000038 RID: 56
private Dictionary<int, int> productTris = new Dictionary<int, int>();
}
}