303 lines
12 KiB
C#
303 lines
12 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using System.Windows;
|
|
|
|
using Autodesk.Revit.Attributes;
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.DB.Plumbing;
|
|
using Autodesk.Revit.UI.Selection;
|
|
|
|
using Nice3point.Revit.Toolkit.External;
|
|
|
|
using ShrlAlgoToolkit.Revit.Assists;
|
|
using ShrlAlgoToolkit.Revit.Extensions;
|
|
|
|
|
|
namespace ShrlAlgo.Addin.Test;
|
|
|
|
[Transaction(TransactionMode.Manual)]
|
|
[Regeneration(RegenerationOption.Manual)]
|
|
public class TempCmd : ExternalCommand
|
|
{
|
|
public override void Execute()
|
|
{
|
|
//导出Revit中的所有族类别BuiltInCategory和中文名
|
|
StringBuilder sb = new StringBuilder();
|
|
var doc = Document;
|
|
Enum.GetValues(typeof(BuiltInCategory)).Cast<BuiltInCategory>().ToList().ForEach(bic =>
|
|
{
|
|
try
|
|
{
|
|
var cat = Category.GetCategory(doc, bic);
|
|
if (cat is { CategoryType: CategoryType.Model, AllowsBoundParameters: true })
|
|
{
|
|
sb.AppendLine($"{cat.Name},{Enum.GetName(typeof(BuiltInCategory), bic)}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
});
|
|
File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "RevitCategory.csv"), sb.ToString(), Encoding.UTF8);
|
|
return;
|
|
if (doc.IsFamilyDocument)
|
|
{
|
|
using (Transaction ts = new Transaction(doc, "删除空类型"))
|
|
{
|
|
ts.Start();
|
|
var manager = doc.FamilyManager;
|
|
foreach (FamilyType type in manager.Types)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(type.Name))
|
|
{
|
|
manager.CurrentType = type;
|
|
doc.Regenerate();
|
|
break;
|
|
}
|
|
}
|
|
manager.DeleteCurrentType();
|
|
ts.Commit();
|
|
}
|
|
}
|
|
return;
|
|
var refer = UiDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
|
|
var element = Document.GetElement(refer);
|
|
Document.Invoke(
|
|
_ =>
|
|
{
|
|
ElementTransformUtils.MoveElement(Document, element.Id, XYZ.BasisY * 200 / 304.8);
|
|
});
|
|
#if false
|
|
var geo = element.get_Geometry(new Options() { ComputeReferences = true, DetailLevel = ViewDetailLevel.Fine });
|
|
if (Document.IsFamilyDocument)
|
|
{
|
|
Solid solid = null;
|
|
var f = Document.OwnerFamily;
|
|
foreach (var item in geo)
|
|
{
|
|
if (item is GeometryInstance instance)
|
|
{
|
|
foreach (var obj in instance.GetInstanceGeometry())
|
|
{
|
|
if (obj is Solid s)
|
|
{
|
|
if (solid == null)
|
|
{
|
|
solid = s;
|
|
}
|
|
if (s.Volume > solid.Volume)
|
|
{
|
|
solid = s;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Document.Invoke(
|
|
ts =>
|
|
{
|
|
DirectShape directShape = DirectShape.CreateElement(Document, f.FamilyCategory.Id);
|
|
directShape.SetShape(new GeometryObject[] { solid });
|
|
});
|
|
//MessageBox.Show(solid.Volume.ToString());
|
|
}
|
|
#endif
|
|
return;
|
|
|
|
//ConnectSprinklers();
|
|
//var unUseSpk = Document.ActiveView.OfClass<FamilyInstance>().OfCategory(BuiltInCategory.OST_Sprinklers).Where(e => e.GetConnectors(true).Size == 1).Select(e => e.Id).ToList();
|
|
//UiDocument.Selection.SetElementIds(unUseSpk);
|
|
//ConnectToStandPipe();
|
|
//var unUseSpk = Document.ActiveView.OfClass<FamilyInstance>().OfCategory(BuiltInCategory.OST_Sprinklers).Where(e => e.GetConnectors(true).Size == 1).ToList();
|
|
//var spks = Document.ActiveView.OfClass<FamilyInstance>().OfCategory(BuiltInCategory.OST_Sprinklers).Where(e => e.GetConnectors(true).Size == 0).ToList();
|
|
//List<Element> li = [];
|
|
//foreach (var item in spks)
|
|
//{
|
|
// var conn = item.GetLocXYZ();
|
|
// foreach (var spk in unUseSpk)
|
|
// {
|
|
// //var conn1 = spk.GetConnectors(true).OfType<Connector>().FirstOrDefault();
|
|
|
|
// if (item.GetLocXYZ().IsAlmostEqualTo(spk.GetLocXYZ()))
|
|
// {
|
|
// li.Add(spk);
|
|
// }
|
|
// }
|
|
//}
|
|
//Document.Invoke(
|
|
// ts =>
|
|
// {
|
|
// Document.Delete(li.Select(e => e.Id).ToList());
|
|
// });
|
|
//MessageBox.Show(sks.Count.ToString());
|
|
}
|
|
|
|
private void ConnectToStandPipe()
|
|
{
|
|
var pipes = Document.ActiveView
|
|
.OfClass<Pipe>()
|
|
.Where(e => e.GetConnectors(true).Size == 1 && (e.GetCurve() as Line).Direction.IsParallelTo(XYZ.BasisZ));
|
|
var spks = Document.ActiveView
|
|
.OfClass<FamilyInstance>()
|
|
.OfCategory(BuiltInCategory.OST_Sprinklers)
|
|
.Where(e => e.GetConnectors(true).Size == 1);
|
|
|
|
Document.Invoke(
|
|
ts =>
|
|
{
|
|
foreach (var ins in spks)
|
|
{
|
|
//Element instance = null;
|
|
var conn1 = ins.GetConnectors(true).OfType<Connector>().FirstOrDefault();
|
|
foreach (var p in pipes)
|
|
{
|
|
var conn = p.GetConnectors(true).OfType<Connector>().FirstOrDefault();
|
|
if (conn.Origin.IsAlmostEqualTo(conn1.Origin))
|
|
{
|
|
conn.ConnectTo(conn1);
|
|
break;
|
|
}
|
|
}
|
|
//try
|
|
//{
|
|
// var connector = ins.GetConnectors(true).OfType<Connector>().FirstOrDefault();
|
|
// var connector1 = instance.GetConnectors(true).OfType<Connector>().FirstOrDefault();
|
|
// var id = ElementTransformUtils.CopyElement(Document, pipe.Id, XYZ.Zero).FirstOrDefault();
|
|
// Document.Regenerate();
|
|
// connector.ConnectTo(newPipe.ConnectorManager.Connectors.GetNearestConnector(connector.Origin));
|
|
// connector1.ConnectTo(newPipe.ConnectorManager.Connectors.GetNearestConnector(connector1.Origin));
|
|
//}
|
|
//catch (Exception)
|
|
//{
|
|
// continue;
|
|
//}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void ConnectSprinklers()
|
|
{
|
|
var fittings = Document.ActiveView
|
|
.OfClass<FamilyInstance>()
|
|
.OfCategory(BuiltInCategory.OST_PipeFitting)
|
|
.Where(e => e.GetConnectors(true).Size == 1);
|
|
var spks = Document.ActiveView
|
|
.OfClass<FamilyInstance>()
|
|
.OfCategory(BuiltInCategory.OST_Sprinklers)
|
|
.Where(e => e.GetConnectors(true).Size == 1);
|
|
var refer = UiDocument.Selection.PickObject(ObjectType.Element);
|
|
var pipe = Document.GetElement(refer) as Pipe;
|
|
Document.Invoke(
|
|
ts =>
|
|
{
|
|
foreach (var ins in spks)
|
|
{
|
|
var temp = double.MaxValue;
|
|
Element instance = null;
|
|
foreach (var ins1 in fittings)
|
|
{
|
|
if (ins.Id == ins1.Id)
|
|
{
|
|
continue;
|
|
}
|
|
var current = ins.GetLocXYZ().DistanceTo(ins1.GetLocXYZ());
|
|
if (current < temp &&
|
|
Math.Abs(current - Math.Abs(ins.GetLocXYZ().Z - ins1.GetLocXYZ().Z)) < 10E-5)
|
|
{
|
|
temp = current;
|
|
instance = ins1;
|
|
}
|
|
}
|
|
try
|
|
{
|
|
var connector = ins.GetConnectors(true).OfType<Connector>().FirstOrDefault();
|
|
var connector1 = instance.GetConnectors(true).OfType<Connector>().FirstOrDefault();
|
|
var id = ElementTransformUtils.CopyElement(Document, pipe.Id, XYZ.Zero).FirstOrDefault();
|
|
var newPipe = Document.GetElement(id) as Pipe;
|
|
newPipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).SetValueString("15");
|
|
(newPipe.Location as LocationCurve).Curve = Line.CreateBound(
|
|
connector.Origin,
|
|
connector1.Origin);
|
|
Document.Regenerate();
|
|
connector.ConnectTo(newPipe.ConnectorManager.Connectors.GetNearestConnector(connector.Origin));
|
|
connector1.ConnectTo(newPipe.ConnectorManager.Connectors.GetNearestConnector(connector1.Origin));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void Method()
|
|
{
|
|
var elemIds = Document.OfClass<Pipe>()
|
|
.Cast<Pipe>()
|
|
.Where(p => p.Diameter == 50 / 304.8 && (p.GetCurve() as Line).Direction.IsParallelTo(XYZ.BasisZ))
|
|
.Select(p => p.Id)
|
|
.ToList();
|
|
//var elementIds = uidoc.Selection.GetElementIds();
|
|
//var elementIds = new FilteredElementCollector(Document).OfClass(typeof(FamilyInstance)).Where(ins => ins.Name == "DN15").Select(ins => ins.Id).ToList();
|
|
//using (var ts = new Transaction(Document, "Temp"))
|
|
//{
|
|
// ts.Start();
|
|
//doc.Delete(elementIds);
|
|
//foreach (var id in elementIds)
|
|
//{
|
|
// var unUseSpk = Document.GetElement(id) as FamilyInstance;
|
|
// var p = unUseSpk.GetLocXYZ();
|
|
// //ElementTransformUtils.MoveElement(Document,id,-unUseSpk.HandOrientation*125/304.8);
|
|
// //Document.Regenerate();
|
|
// //ElementTransformUtils.MoveElement(Document, id,-unUseSpk.FacingOrientation*95/304.8);
|
|
// ElementTransformUtils.RotateElement(
|
|
// Document,
|
|
// id,
|
|
// Line.CreateUnbound(p, XYZ.BasisZ),
|
|
// Math.PI / 2);
|
|
//}
|
|
//内衬
|
|
//var e = InsulationLiningBase.GetLiningIds(doc, unUseSpk.Id);
|
|
//保温
|
|
//var p = InsulationLiningBase.GetInsulationIds(doc, unUseSpk.Id);
|
|
//var uidoc = UiApplication.ActiveUIDocument;
|
|
//var doc = uidoc.Document;
|
|
//var reference = uidoc.Selection.PickObject(ObjectType.Element);
|
|
//var unUseSpk = doc.GetElement(reference);
|
|
//var id = InsulationLiningBase.GetInsulationIds(Document, unUseSpk.Id).FirstOrDefault();
|
|
// ts.Commit();
|
|
//}
|
|
Document.Invoke(
|
|
ts =>
|
|
{
|
|
try
|
|
{
|
|
//UiDocument.Selection.SetElementIds(elemIds);
|
|
|
|
//ElementTransformUtils.MoveElements(Document, elemIds, XYZ.BasisZ * 200 / 304.8);
|
|
|
|
foreach (var id in elemIds)
|
|
{
|
|
var e = Document.GetElement(id) as Pipe;
|
|
if (e != null)
|
|
{
|
|
var b = e.GetCurve().GetEndPoint(0);
|
|
if (b.Z == 0)
|
|
{
|
|
e.GetLocationCurve().Curve = Line.CreateBound(
|
|
b + XYZ.BasisZ * 1800 / 304.8,
|
|
e.GetCurve().GetEndPoint(1));
|
|
continue;
|
|
}
|
|
}
|
|
//unUseSpk.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(50 / 304.8);
|
|
}
|
|
}
|
|
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
|
{
|
|
}
|
|
},
|
|
"CMD");
|
|
}
|
|
} |