2025-09-16 16:06:41 +08:00
2025-12-23 21:37:02 +08:00
using System.Data ;
using System.Linq ;
using System.Windows ;
2025-09-16 16:06:41 +08:00
using Autodesk.Revit.Attributes ;
using Autodesk.Revit.DB ;
using Autodesk.Revit.UI ;
using Microsoft.Win32 ;
using Newtonsoft.Json ;
using Nice3point.Revit.Toolkit.External ;
using Szmedi.RvKits.ExportFamilyInfo ;
namespace Szmedi.RvKits.FamilyTools
{
[Transaction(TransactionMode.Manual)]
public class ComponentInfoCmd : ExternalCommand
{
public override void Execute ( )
{
//AssemblyLoaderHelpers loader = new(GlobalVariables.DirAssembly);
ExportSettingsWin settings = null ;
settings = new ExportSettingsWin ( ) ;
settings . ShowDialog ( ) ;
//loader.HookAssemblyResolve();
//try
//{
//}
//catch (Exception ex)
//{
// LogAssists.WriteLog(ex.Message);
// Result = Result.Failed;
//}
//finally
//{
// loader.UnhookAssemblyResolve();
//}
if ( settings . DialogResult ! = true )
{
Result = Result . Cancelled ;
return ;
}
OpenFileDialog openFileDialog = new ( )
{
Filter = "<22> <> <EFBFBD> ļ<EFBFBD> (*.rfa)|*.rfa" ,
Multiselect = true
} ;
var savepath = Properties . Settings . Default . SavePath ;
//Autodesk.Revit.UI.FileOpenDialog fileOpenDialog = new FileOpenDialog("<22> <> <EFBFBD> ļ<EFBFBD> (*.rfa)|*.rfa");
//fileOpenDialog.Show();
//fileOpenDialog.GetSelectedModelPath();
if ( openFileDialog . ShowDialog ( ) = = true )
{
foreach ( var filename in openFileDialog . FileNames )
{
var famdoc = Application . OpenDocumentFile ( filename ) ;
var dictJson = DictParas ( famdoc ) ;
try
{
DictionaryToJson ( filename , dictJson , savepath ) ;
}
catch ( Exception ex )
{
LogAssists . WriteLog ( ex . Message ) ;
}
ExportThumbnail ( savepath , filename , famdoc ) ;
famdoc . Close ( false ) ;
var famFullPath = Path . Combine ( savepath , Path . GetFileNameWithoutExtension ( filename ) , Path . GetFileName ( filename ) ) ;
//DirectoryInfo FamDir = new DirectoryInfo(FamPath);
//if (!FamDir.Exists)
//{
// FamDir.Create();
//}
//File.Move(fullPath, FamFullPath);
FileInfo file = new ( famFullPath ) ;
if ( file . Exists )
{
file . Delete ( ) ;
}
File . Copy ( filename , famFullPath ) ;
}
}
}
private DataTable DtParasToExcel ( Document doc , FamilyManager fm ) //<2F> <> ȡ<EFBFBD> <C8A1> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD>
{
//StringBuilder sb = new StringBuilder();//<2F> õ<EFBFBD> <C3B5> <EFBFBD> <EFBFBD> е <EFBFBD> ö<EFBFBD> <C3B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
//sb.Append(famdoc.Title + "\r\n");
var ft = fm . CurrentType ; //<2F> <> ȡ<EFBFBD> <C8A1> ǰ<EFBFBD> <C7B0> <EFBFBD> <EFBFBD>
DataTable dt = new ( ) ;
//dt.Columns.Add("<22> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ", typeof(string));
dt . Columns . Add ( "<22> <> <EFBFBD> <EFBFBD> " , typeof ( string ) ) ;
dt . Columns . Add ( "<22> <> <EFBFBD> <EFBFBD> ֵ" , typeof ( string ) ) ;
dt . Columns . Add ( "<22> <> ʽ " , typeof ( string ) ) ;
if ( ft = = null )
{
//IList<FamilyParameter> faparas = fm.GetParameters();
//foreach (var para in faparas)
//{
// sb.Append(para.Definition.Name);
//}
using Transaction trans = new ( doc ) ;
trans . Start ( "<22> ½<EFBFBD> <C2BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> " ) ;
var NT = "NewType" ;
ft = fm . NewType ( NT ) ;
trans . Commit ( ) ;
}
List < BuiltInParameterGroup > pgs = [ ] ;
foreach ( FamilyParameter para in fm . Parameters )
{
if ( ! pgs . Contains ( para . Definition . ParameterGroup ) )
{
pgs . Add ( para . Definition . ParameterGroup ) ;
}
}
foreach ( var pg in pgs )
{
var drpg = dt . NewRow ( ) ;
drpg [ 0 ] = null ;
drpg [ 1 ] = LabelUtils . GetLabelFor ( pg ) ;
drpg [ 2 ] = null ;
dt . Rows . Add ( drpg ) ;
foreach ( FamilyParameter para in fm . Parameters )
{
if ( para . Definition . ParameterGroup = = pg )
{
var drpara = dt . NewRow ( ) ;
drpara [ 0 ] = para . Definition . Name ;
drpara [ 1 ] = ft . AsValueString ( para ) ;
drpara [ 2 ] = para . Formula ;
dt . Rows . Add ( drpara ) ;
}
}
}
return dt ;
//foreach (FamilyParameter para in fm.Parameters)
//{
// DataRow dr = dt.NewRow();
// dr[0] = LabelUtils.GetLabelFor(para.Definition.ParameterGroup);
// dr[1] = para.Definition.Name;
// dr[2] = ft.AsValueString(para);
// dt.Rows.Add(dr);
// //sb.Append(para.Definition.ParameterGroup + ":" + para.Definition.Name + ":" + ft.AsValueString(para) + "\r\n");
// //switch (para.StorageType)
// //{
// // case StorageType.Double:
// // sb.Append(para.Definition.Name + ":" + ft.AsDouble(para) * 304.8 + "\r\n");
// // break;
// // case StorageType.Integer:
// // sb.Append(para.Definition.Name + ":" + ft.AsInteger(para) * 304.8 + "\r\n");
// // break;
// // case StorageType.ElementId:
// // sb.Append(para.Definition.Name + ":" + ft.AsElementId(para) + "\r\n");
// // break;
// // case StorageType.String:
// // sb.Append(para.Definition.Name + ":" + ft.AsString(para) + "\r\n");
// // break;
// // case StorageType.None:
// // break;
// //}
//}
//TaskDialog.Show(header, sb.ToString());
}
private DataTable DtParasToJson ( Document doc ) //<2F> <> ȡ<EFBFBD> <C8A1> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD>
{
//StringBuilder sb = new StringBuilder();//<2F> õ<EFBFBD> <C3B5> <EFBFBD> <EFBFBD> е <EFBFBD> ö<EFBFBD> <C3B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
//sb.Append(famdoc.Title + "\r\n");
var fm = doc . FamilyManager ;
var ft = fm . CurrentType ; //<2F> <> ȡ<EFBFBD> <C8A1> ǰ<EFBFBD> <C7B0> <EFBFBD> <EFBFBD>
DataTable dt = new ( ) ;
dt . Columns . Add ( "<22> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> " , typeof ( string ) ) ;
dt . Columns . Add ( "<22> <> <EFBFBD> <EFBFBD> " , typeof ( string ) ) ;
dt . Columns . Add ( "<22> <> <EFBFBD> <EFBFBD> ֵ" , typeof ( string ) ) ;
dt . Columns . Add ( "<22> <> ʽ " , typeof ( string ) ) ;
if ( ft = = null )
{
//IList<FamilyParameter> faparas = fm.GetParameters();
//foreach (var para in faparas)
//{
// sb.Append(para.Definition.Name);
//}
using Transaction trans = new ( doc ) ;
trans . Start ( "<22> ½<EFBFBD> <C2BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> " ) ;
var NT = "NewType" ;
ft = fm . NewType ( NT ) ;
trans . Commit ( ) ;
}
List < BuiltInParameterGroup > pgs = [ ] ;
foreach ( FamilyParameter para in fm . Parameters )
{
if ( ! pgs . Contains ( para . Definition . ParameterGroup ) )
{
pgs . Add ( para . Definition . ParameterGroup ) ;
}
}
foreach ( var pg in pgs )
{
foreach ( FamilyParameter para in fm . Parameters )
{
if ( para . Definition . ParameterGroup = = pg )
{
var drpara = dt . NewRow ( ) ;
drpara [ 0 ] = LabelUtils . GetLabelFor ( pg ) ;
drpara [ 1 ] = para . Definition . Name ;
drpara [ 2 ] = ft . AsValueString ( para ) ;
drpara [ 3 ] = para . Formula ;
dt . Rows . Add ( drpara ) ;
}
}
}
return dt ;
}
/// <summary>
/// <20> <> ȡתJson<6F> IJ<EFBFBD> <C4B2> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
/// </summary>
/// <param name="doc"></param>
/// <returns></returns>
private Dictionary < string , Dictionary < string , JsonPara > > DictParas ( Document doc )
{
var fm = doc . FamilyManager ;
var ft = fm . CurrentType ; //<2F> <> ȡ<EFBFBD> <C8A1> ǰ<EFBFBD> <C7B0> <EFBFBD> <EFBFBD>
//<2F> <> <EFBFBD> 飬dir<<3C> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> >
Dictionary < string , Dictionary < string , JsonPara > > paragroups = [ ] ;
using Transaction trans = new ( doc ) ;
trans . Start ( "<22> ½<EFBFBD> <C2BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> " ) ;
ft ? ? = fm . NewType ( "NewType" ) ;
List < BuiltInParameterGroup > pgs = [ ] ;
foreach ( FamilyParameter para in fm . Parameters )
{
if ( ! pgs . Contains ( para . Definition . ParameterGroup ) )
{
pgs . Add ( para . Definition . ParameterGroup ) ;
}
}
foreach ( var pg in pgs )
{
Dictionary < string , JsonPara > paras = [ ] ;
foreach ( FamilyParameter para in fm . Parameters )
{
if ( para . Definition . ParameterGroup = = pg )
{
JsonPara JP = new ( ) { Value = ft . AsValueString ( para ) , Formula = para . Formula } ;
paras . Add ( para . Definition . Name , JP ) ;
//drpara[0] = para.Definition.Name;
//drpara[1] = ft.AsValueString(para);
//drpara[2] = para.Formula;
}
}
paragroups . Add ( LabelUtils . GetLabelFor ( pg ) , paras ) ;
}
trans . RollBack ( ) ;
return paragroups ;
}
private void DictionaryToJson ( string fullName , Dictionary < string , Dictionary < string , JsonPara > > dicts , string DirChoosed )
{
//string filename = "";
//try
//{
// FileInfo file = new FileInfo(filename);
// string json = File.ReadAllText("config.json");
// dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
// //jsonObj["L_BPointMoveDelay"] = LBPointdelay.ToString();
// string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
// File.WriteAllText("config.json", output);
//}
//catch (Exception)
//{
// throw;
//}
//string serverAppPath = Request.PhysicalApplicationPath.ToString();
//<2F> <> ȡ<EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ·<EFBFBD> <C2B7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> չ<EFBFBD> <D5B9> <EFBFBD> <EFBFBD>
var safefilename = Path . GetFileNameWithoutExtension ( fullName ) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> չ<EFBFBD> <D5B9> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD> ·<EFBFBD> <C2B7> +<2B> ļ<EFBFBD> <C4BC> <EFBFBD>
//string JosnWithoutEx = DirChoosed + "\\" + safefilename + "\\" + safefilename + "-" + "<22> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ";
//<2F> <> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ŀ¼<C4BF> <C2BC> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD> Ŀ¼<C4BF> <C2BC>
var fileDir = Path . Combine ( DirChoosed , safefilename ) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ·<EFBFBD> <C2B7> <EFBFBD> <EFBFBD>
var file_fullpath = Path . Combine ( DirChoosed , safefilename , $"{safefilename}.json" ) ;
if ( ! Directory . Exists ( fileDir ) )
{
Directory . CreateDirectory ( fileDir ) ;
}
if ( ! File . Exists ( file_fullpath ) )
{
//File.Create(file_fullpath);
var JsonString = JsonConvert . SerializeObject ( dicts ) ;
File . WriteAllText ( file_fullpath , JsonString ) ;
}
//<2F> <> ģ<EFBFBD> <C4A3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> д<EFBFBD> <D0B4> <EFBFBD> ļ<EFBFBD>
//using (StreamWriter sw = new StreamWriter(file_fullpath))
//{
// JsonSerializer serializer = new JsonSerializer();
// serializer.Converters.Add(new JavaScriptDateTimeConverter());
// serializer.NullValueHandling = NullValueHandling.Ignore;
// //<2F> <> <EFBFBD> <EFBFBD> Json.net<65> <74> д<EFBFBD> <D0B4> <EFBFBD> <EFBFBD>
// JsonWriter writer = new JsonTextWriter(sw);
// //<2F> <> ģ<EFBFBD> <C4A3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> л<EFBFBD> <D0BB> <EFBFBD> д<EFBFBD> <D0B4> Json.net<65> <74> JsonWriter<65> <72> <EFBFBD> <EFBFBD>
// serializer.Serialize(writer, lot);
// //ser.Serialize(writer, ht);
// writer.Close();
// sw.Close();
//}
}
/// <summary>
/// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ѡ <EFBFBD> <D1A1>
/// </summary>
/// <param name="saveDirChoosed"></param>
/// <param name="filesFullPathChoosed"></param>
/// <param name="famodc"></param>
/// <param name="views"></param>
/// <param name="forward"></param>
private void ExportOption ( string saveDirChoosed , string filesFullPathChoosed , Document famodc , IList < ElementId > views , XYZ forward )
{
var Imapath = Path . Combine ( saveDirChoosed , Path . GetFileNameWithoutExtension ( filesFullPathChoosed ) ) ;
var format = Properties . Settings . Default . ImgFormat ;
if ( ! Directory . Exists ( Imapath ) )
{
Directory . CreateDirectory ( Imapath ) ;
}
ImageExportOptions options =
new ( )
{
FilePath = Path . Combine ( Imapath , $"{forward}" ) ,
FitDirection = FitDirectionType . Horizontal ,
HLRandWFViewsFileType = ( ImageFileType ) Enum . Parse ( typeof ( ImageFileType ) , format ) ,
//HLRandWFViewsFileType = ImageFileType.JPEGLossless,
//ShadowViewsFileType = (ImageFileType)Enum.Parse(typeof(ImageFileType), format),
ExportRange = ExportRange . SetOfViews ,
ImageResolution = ImageResolution . DPI_600 ,
//ShouldCreateWebSite = false,
ZoomType = ZoomFitType . FitToPage ,
PixelSize = int . Parse ( Properties . Settings . Default . ImgSize )
} ;
//if (views.Count > 0)
//{
// options.SetViewsAndSheets(views);
// options.ExportRange = ExportRange.SetOfViews;
//}
//else
//{
// options.ExportRange = ExportRange
// .VisibleRegionOfCurrentView;
//}
options . SetViewsAndSheets ( views ) ;
famodc . ExportImage ( options ) ;
}
private void ExportThumbnail ( string saveDirChoosed , string filesFullPathChoosed , Document famdoc )
{
IList < ElementId > views = [ ] ;
//FilteredElementCollector concol = new FilteredElementCollector(famdoc).QueryElementsByType(typeof(ConnectorElement)).OfCategory(BuiltInCategory.OST_ConnectorElem);
//FilteredElementCollector dimcol = new FilteredElementCollector(famdoc).QueryElementsByType(typeof(Dimension)).OfCategory(BuiltInCategory.OST_Dimensions);
var v3d = new FilteredElementCollector ( famdoc )
. OfClass ( typeof ( View ) )
. OfCategory ( BuiltInCategory . OST_Views )
. OfType < View3D > ( )
. FirstOrDefault ( x = > x . ViewType = = ViewType . ThreeD ) ;
//ElementClassFilter dimcf = new ElementClassFilter(typeof(Dimension));
//ElementClassFilter ardimcf = new ElementClassFilter(typeof(AngularDimension));
//LogicalOrFilter orFilter = new LogicalOrFilter(dimcf, ardimcf);
// FilteredElementCollector dimcol = new FilteredElementCollector(famdoc).WherePasses(orFilter);
//var viewFamilyType = new FilteredElementCollector(famdoc).QueryElementsByType(typeof(ViewFamilyType)).OfType<ViewFamilyType>().LastOrDefault(x => x.ViewFamily == ViewFamily.ThreeDimensional);
//var viewFamilyTypes = new FilteredElementCollector(famdoc).QueryElementsByType(typeof(ViewFamilyType)).OfType<ViewFamilyType>().GetEnumerator();
//var direction = new XYZ(-1, 1, -1);
using Transaction trans = new ( famdoc , "ExportImage" ) ;
trans . Start ( ) ;
//XYZ eyep1 = new XYZ(1.66983442269142, -1.62882392400378, 6.75513625996179);
//XYZ forwarddir1 = new XYZ(0.577350269189626, 0.577350269189626, -0.577350269189626);
//XYZ updir1 = new XYZ(0.408248290463863, 0.408248290463863, 0.816496580927726);
//ViewOrientation3D isometric1 = new ViewOrientation3D(eyep1, forwarddir1, updir1);
//XYZ eyep2 = new XYZ(6.66133814775094E-15, 2.22788354954364, 4.88410451942765);
//XYZ forwarddir2 = new XYZ(0, -1, 0);
//XYZ updir2 = new XYZ(0, 0, 1);
//ViewOrientation3D isometric2 = new ViewOrientation3D(eyep2, forwarddir2, updir2);
//var coord = v3d.GetOrientation();
var graphicDisplayOptions = v3d . get_Parameter ( BuiltInParameter . MODEL_GRAPHICS_STYLE ) ;
graphicDisplayOptions . Set ( 8 ) ;
var elemIds = new FilteredElementCollector ( famdoc )
. OfClass ( typeof ( ConnectorElement ) ) . ToElementIds ( ) ;
var elemIds1 = new FilteredElementCollector ( famdoc )
. OfCategory ( BuiltInCategory . OST_Dimensions ) . ToElementIds ( ) ;
v3d . HideElements ( elemIds ) ;
v3d . HideElements ( elemIds1 ) ;
//ģ<> <C4A3> <EFBFBD> <EFBFBD> ʾ Ϊ<CABE> <CEAA> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
//Options op = new Options();
//op.DetailLevel = ViewDetailLevel.Fine;
//op.View.DisplayStyle = DisplayStyle.Realistic;
//famdoc.ActiveView = v3d;
views . Add ( v3d . Id ) ;
trans . Commit ( ) ;
//v3d.SetOrientation(new ViewOrientation3D(new XYZ(-1, 1, -1), new XYZ(0, 1, 1), new XYZ(0, 1, -1)));
XYZ forward = new ( - 1 , 1 , - 1 ) ;
v3d . OrientTo ( forward ) ;
ExportOption ( saveDirChoosed , filesFullPathChoosed , famdoc , views , forward ) ;
//v3d.OrientTo(new XYZ(0, -1, 0));
//ExportOption(ps, saveDirChoosed, filesFullPathChoosed, famdoc, views);
#region MyRegion
//while (viewFamilyTypes.MoveNext()) //һ <> <D2BB> Ҫ<EFBFBD> <D2AA> movenext,<2C> <> Ȼ<EFBFBD> <C8BB> <EFBFBD> <EFBFBD> <EFBFBD> 쳣
//{
// if (viewFamilyTypes.Current.ViewFamily == ViewFamily.ThreeDimensional)
// {
// var view3D = View3D.CreateIsometric(famdoc, viewFamilyTypes.Current.Id);
// //var view3D = (viewFamilyType != null) ? View3D.CreateIsometric(famdoc, viewFamilyType.Id) : null;
// //var viewer = view3D.GetOrientation();
// if (view3D != null)
// {
// if (concol != null)
// {
// famdoc.Delete(concol.ToElementIds());
// //famdoc.Delete(dimcol.ToElementIds());
// }
// if (dimcol != null)
// {
// foreach (Dimension dim in dimcol.ToElements())
// {
// if (dim.ValueString != null)
// {
// //System.Diagnostics.Debug.WriteLine(dim.Id);
// try
// {
// famdoc.Delete(dim.Id);
// }
// catch (Exception)
// {
// //System.Diagnostics.Debug.WriteLine(dim.Id);
// }
// }
// }
// }
// views.Add(view3D.Id);
// var graphicDisplayOptions
// = view3D.get_Parameter(
// BuiltInParameter.MODEL_GRAPHICS_STYLE);
// //ģ<> <C4A3> <EFBFBD> <EFBFBD> ʾ Ϊ<CABE> <CEAA> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
// graphicDisplayOptions.Set(6);
// }
// }
//}
#endregion
forward = new XYZ ( 1 , - 1 , - 1 ) ;
v3d . OrientTo ( forward ) ;
ExportOption ( saveDirChoosed , filesFullPathChoosed , famdoc , views , forward ) ;
forward = new XYZ ( 0 , 1 , 0 ) ;
v3d . OrientTo ( forward ) ;
ExportOption ( saveDirChoosed , filesFullPathChoosed , famdoc , views , forward ) ;
//ExportOption(ps, saveDirChoosed, filesFullPathChoosed, famdoc, views);
}
}
}