2024-09-22 11:05:41 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
using Autodesk.Revit.Attributes;
|
|
|
|
|
|
using Autodesk.Revit.DB;
|
2026-02-20 16:47:26 +08:00
|
|
|
|
using Melskin.Controls;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
|
using Nice3point.Revit.Toolkit.External;
|
2025-04-24 20:56:44 +08:00
|
|
|
|
using ShrlAlgoToolkit.RevitAddins.Windows;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
using TaskDialog = Autodesk.Revit.UI.TaskDialog;
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
namespace ShrlAlgoToolkit.RevitAddins.RvFamily;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
[Transaction(TransactionMode.Manual)]
|
|
|
|
|
|
public class UpgradeFamilyCmd : ExternalCommand
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
private List<string> fileNames;
|
|
|
|
|
|
|
|
|
|
|
|
public override void Execute()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
fileNames = GetSelectedFileNames();
|
|
|
|
|
|
if (fileNames.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var progressMonitorViewModel = new ProgressBarManager<string>(UiDocument, fileNames, UpdateFile, "更新文件");
|
|
|
|
|
|
progressMonitorViewModel.ProgressModal();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (e is not Autodesk.Revit.Exceptions.OperationCanceledException)
|
|
|
|
|
|
{
|
|
|
|
|
|
ErrorMessage = e.Message;
|
|
|
|
|
|
Result = Result.Failed;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取选择全路径文件名列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static List<string> GetSelectedFileNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
var taskDialog = new TaskDialog("选择方式")
|
|
|
|
|
|
{
|
|
|
|
|
|
MainInstruction = "选择方式",
|
|
|
|
|
|
MainContent = "请选择方式"
|
|
|
|
|
|
//FooterText = "<a href=\"http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=2484975 \">"
|
|
|
|
|
|
//+ "进入revit开发中心</a>"
|
|
|
|
|
|
};
|
|
|
|
|
|
taskDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "选择文件");
|
|
|
|
|
|
taskDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "选择文件夹");
|
|
|
|
|
|
var tResult = taskDialog.Show();
|
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
switch (tResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
case TaskDialogResult.CommandLink1:
|
|
|
|
|
|
{
|
|
|
|
|
|
var filter = "族文件|*.rfa";
|
|
|
|
|
|
var openFile = new OpenFileDialog
|
|
|
|
|
|
{
|
|
|
|
|
|
Multiselect = true,
|
|
|
|
|
|
Filter = filter
|
|
|
|
|
|
};
|
|
|
|
|
|
openFile.ShowDialog();
|
|
|
|
|
|
if (openFile.FileNames.Length != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var strFiles = openFile.FileNames;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var file in strFiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
list.Add(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case TaskDialogResult.CommandLink2:
|
|
|
|
|
|
{
|
|
|
|
|
|
var dialog = new VistaFolderBrowserDialog
|
|
|
|
|
|
{
|
|
|
|
|
|
Multiselect = false
|
|
|
|
|
|
};
|
|
|
|
|
|
dialog.ShowDialog();
|
|
|
|
|
|
|
|
|
|
|
|
if (dialog.SelectedPath != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var directoryInfo = new DirectoryInfo(dialog.SelectedPath);
|
|
|
|
|
|
var files = directoryInfo.GetFiles("*.rfa", SearchOption.AllDirectories);
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
list.Add(file.FullName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateFile(UIDocument uidoc, string filename, object obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.Threading.Thread.Sleep(50);
|
|
|
|
|
|
|
|
|
|
|
|
if (File.GetAttributes(filename).ToString().IndexOf("ReadOnly", StringComparison.Ordinal) != -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
File.SetAttributes(filename, FileAttributes.Normal);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Document famdoc;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
famdoc = Application.OpenDocumentFile(filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Autodesk.Revit.Exceptions.CorruptModelException)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (famdoc.IsFamilyDocument)
|
|
|
|
|
|
{
|
|
|
|
|
|
using var trans = new Transaction(famdoc);
|
|
|
|
|
|
trans.Start("删除多余属性值");
|
|
|
|
|
|
var fm = famdoc.FamilyManager;
|
|
|
|
|
|
if (fm.CurrentType == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var t = Path.GetFileNameWithoutExtension(filename);
|
|
|
|
|
|
fm.NewType(t);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var familyParameter = fm.get_Parameter(BuiltInParameter.ALL_MODEL_URL);
|
|
|
|
|
|
if (familyParameter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
fm.Set(familyParameter, string.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
familyParameter = fm.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS);
|
|
|
|
|
|
if (familyParameter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
fm.Set(familyParameter, string.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
familyParameter = fm.get_Parameter(BuiltInParameter.ALL_MODEL_DESCRIPTION);
|
|
|
|
|
|
if (familyParameter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
fm.Set(familyParameter, string.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
familyParameter = fm.get_Parameter(BuiltInParameter.KEYNOTE_PARAM);
|
|
|
|
|
|
if (familyParameter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
fm.Set(familyParameter, string.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
familyParameter = fm.get_Parameter(BuiltInParameter.UNIFORMAT_CODE);
|
|
|
|
|
|
if (familyParameter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
fm.Set(familyParameter, string.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
familyParameter = fm.get_Parameter(BuiltInParameter.ALL_MODEL_MODEL);
|
|
|
|
|
|
if (familyParameter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
fm.Set(familyParameter, string.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var fampara = fm.get_Parameter("族库大师");
|
|
|
|
|
|
if (fampara != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
fm.RemoveParameter(fampara);
|
|
|
|
|
|
}
|
|
|
|
|
|
var hostobjects = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfClass(typeof(HostObject)).ToElementIds();
|
|
|
|
|
|
var rplanes = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfCategory(BuiltInCategory.OST_CLines).ToElementIds();
|
|
|
|
|
|
var dimids = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfClass(typeof(Dimension)).ToElementIds();
|
|
|
|
|
|
var conids = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfClass(typeof(ConnectorElement)).ToElementIds();
|
|
|
|
|
|
//ICollection<ElementId> curids = new FilteredElementCollector(famdoc).OfClass(typeof(CurveElement)).ToElementIds();
|
|
|
|
|
|
var textnotes = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfClass(typeof(TextNote)).ToElementIds();
|
|
|
|
|
|
var lightsources = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfCategory(BuiltInCategory.OST_LightingFixtureSource).ToElementIds();
|
|
|
|
|
|
var lights = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfCategory(BuiltInCategory.OST_LightingFixtures).ToElementIds();
|
|
|
|
|
|
var sketchgrids = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfCategory(BuiltInCategory.OST_IOSSketchGrid).ToElementIds();
|
|
|
|
|
|
var modelcurves = new FilteredElementCollector(famdoc)
|
|
|
|
|
|
.OfCategory(BuiltInCategory.OST_ReferenceLines).ToElementIds();
|
|
|
|
|
|
var facebox =
|
|
|
|
|
|
new FilteredElementCollector(famdoc).OfClass(typeof(Extrusion));
|
|
|
|
|
|
//UiDocument.ActiveView = v3d;
|
|
|
|
|
|
//UIView uIView = UiDocument.GetOpenUIViews().Where(x => x.ViewId == v3d.ViewId).FirstOrDefault();
|
|
|
|
|
|
//uIView.ZoomToFit();
|
|
|
|
|
|
var setting = famdoc.GetDocumentPreviewSettings();
|
|
|
|
|
|
var previewid = famdoc.FindPreviewId<View3D>(setting);
|
|
|
|
|
|
setting.PreviewViewId = previewid;
|
|
|
|
|
|
var v = famdoc.GetElement(previewid) as View;
|
|
|
|
|
|
setting.ForceViewUpdate(true);
|
|
|
|
|
|
//得到基于面的族样板拉伸体
|
|
|
|
|
|
if (facebox.Count() != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var boxs = from x in facebox
|
|
|
|
|
|
where Math.Abs(x.get_Parameter(BuiltInParameter.EXTRUSION_END_PARAM).AsDouble() - -1) < 0.0001
|
|
|
|
|
|
select x.Id;
|
|
|
|
|
|
var elementIds = boxs.ToList();
|
|
|
|
|
|
if (elementIds.Count() != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
v.HideElementsTemporary(elementIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//var graphicDisplayOptions = v3d.get_Parameter(BuiltInParameter.MODEL_GRAPHICS_STYLE);
|
|
|
|
|
|
//graphicDisplayOptions.Set(8);
|
|
|
|
|
|
|
|
|
|
|
|
v.DisplayStyle = DisplayStyle.RealisticWithEdges;
|
|
|
|
|
|
v.DetailLevel = ViewDetailLevel.Fine;
|
|
|
|
|
|
ThinLinesOptions.AreThinLinesEnabled = true;
|
|
|
|
|
|
v.HideElementsTemporary(hostobjects);
|
|
|
|
|
|
v.HideElementsTemporary(rplanes);
|
|
|
|
|
|
v.HideElementsTemporary(dimids);
|
|
|
|
|
|
v.HideElementsTemporary(conids);
|
|
|
|
|
|
v.HideElementsTemporary(textnotes);
|
|
|
|
|
|
v.HideElementsTemporary(lights);
|
|
|
|
|
|
v.HideElementsTemporary(lightsources);
|
|
|
|
|
|
v.HideElementsTemporary(sketchgrids);
|
|
|
|
|
|
v.HideElementsTemporary(modelcurves);
|
|
|
|
|
|
#if REVIT2018 || REVIT2020
|
|
|
|
|
|
var ele = famdoc.GetElement(new ElementId(1537));
|
|
|
|
|
|
#elif REVIT2025
|
|
|
|
|
|
var ele = famdoc.GetElement(ElementId.Parse("1537"));
|
|
|
|
|
|
#endif
|
|
|
|
|
|
if (ele != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
v.HideElementTemporary(ele.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (v is View3D)
|
|
|
|
|
|
{
|
|
|
|
|
|
var v3d = v as View3D;
|
|
|
|
|
|
if (v3d.IsLocked)
|
|
|
|
|
|
{
|
|
|
|
|
|
v3d.Unlock();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (famdoc.OwnerFamily.FamilyCategory ==
|
|
|
|
|
|
Category.GetCategory(famdoc, BuiltInCategory.OST_LightingFixtures))
|
|
|
|
|
|
{
|
|
|
|
|
|
v3d.OrientTo(new XYZ(-1, 1, 1));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//人看向视图的视角向量
|
|
|
|
|
|
//v3d.OrientTo(new XYZ(-1, 1, -1));
|
|
|
|
|
|
v3d.OrientTo(new XYZ(-1, -1, -1));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//v3d.OrientTo(new XYZ(1, -1, -1));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//XYZ eyeposition = new XYZ();
|
|
|
|
|
|
//XYZ updirection = XYZ.BasisZ;
|
|
|
|
|
|
//XYZ forwarddirection = XYZ.BasisY;
|
|
|
|
|
|
//ViewOrientation3D orientation3D = new ViewOrientation3D(eyeposition,updirection,forwarddirection);
|
|
|
|
|
|
//v3d.SetOrientation(orientation3D);
|
|
|
|
|
|
//UiDocument.RefreshActiveView();
|
|
|
|
|
|
//v3d.ToggleToIsometric();
|
|
|
|
|
|
//System.Windows.Forms.SendKeys.SendWait("TL");
|
|
|
|
|
|
ThinLinesOptions.AreThinLinesEnabled = true;
|
|
|
|
|
|
var sa = new SaveAsOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
OverwriteExistingFile = true,
|
|
|
|
|
|
PreviewViewId = previewid
|
|
|
|
|
|
};
|
|
|
|
|
|
//SetViewSaveOption(famdoc, v3d, sa, setting);
|
|
|
|
|
|
|
|
|
|
|
|
trans.Commit();
|
|
|
|
|
|
famdoc.SaveAs(filename, sa);
|
|
|
|
|
|
famdoc.Close(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
//if (ex.GetType() == typeof(OperationCanceledException))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (trans.HasStarted())
|
|
|
|
|
|
// {
|
|
|
|
|
|
// trans.Commit();
|
|
|
|
|
|
// famdoc.CloseTrigger(false);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// break;
|
|
|
|
|
|
//}
|
|
|
|
|
|
if (trans.HasStarted())
|
|
|
|
|
|
{
|
|
|
|
|
|
trans.Commit();
|
|
|
|
|
|
famdoc.Close(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else //不是族文件时
|
|
|
|
|
|
{
|
|
|
|
|
|
var sa = new SaveAsOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
OverwriteExistingFile = true
|
|
|
|
|
|
};
|
|
|
|
|
|
famdoc.SaveAs(filename, sa);
|
|
|
|
|
|
famdoc.Close(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
var isMatch = Regex.IsMatch(filename, ".\\d{4}(.rfa)");
|
|
|
|
|
|
if (isMatch)
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|