整理代码
This commit is contained in:
110
ShrlAlgoToolkit.RevitAddins/RvMEP/CableLayoutCmd.cs
Normal file
110
ShrlAlgoToolkit.RevitAddins/RvMEP/CableLayoutCmd.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using Autodesk.Revit.Attributes;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.DB.Electrical;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
|
||||
using Nice3point.Revit.Toolkit.External;
|
||||
|
||||
|
||||
|
||||
namespace ShrlAlgo.RvKits.RvMEP;
|
||||
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
[Regeneration(RegenerationOption.Manual)]
|
||||
public class CableLayoutCmd : ExternalCommand
|
||||
{
|
||||
private readonly List<Element> elems = new();
|
||||
|
||||
public override void Execute()
|
||||
{
|
||||
CableLayoutView view = new(Document);
|
||||
|
||||
if (view.ShowDialog() != true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var count = view.ViewModel.Count;
|
||||
var conduitType = view.ViewModel.SelectedConduitType;
|
||||
var size = view.ViewModel.Size;
|
||||
Document.Invoke(
|
||||
_ =>
|
||||
{
|
||||
//Reference refer = UiDocument.Selection.PickObject(ObjectType.ElementToMove, new TypeSelectionFilter<CableTray>(), "请选择桥架");
|
||||
//var ct = UiDocument.Document.GetElement(refer) as CableTray;
|
||||
var refer = UiDocument.Selection.PickObject(ObjectType.Element, new GenericFilter<CableTray>(), "请选择需要敷设桥架");
|
||||
var ct = UiDocument.Document.GetElement(refer) as MEPCurve;
|
||||
var xyz = refer.GlobalPoint;
|
||||
//Curve curve = (ct.Location as LocationCurve).Curve;
|
||||
//XYZ project = curve.ProjectOf(xyz).XYZPoint;
|
||||
elems.Add(ct);
|
||||
var connector = ct.ConnectorManager.Connectors.GetNearestConnector(xyz);
|
||||
connector.GetAllRefsElements(elems);
|
||||
Dictionary<int, List<Conduit>> dictionary = new();
|
||||
List<Conduit> firstConduits = new();
|
||||
var interval = size.OuterDiameter;
|
||||
foreach (var elem in elems)
|
||||
{
|
||||
if (elem is MEPCurve mepCurve)
|
||||
{
|
||||
var conduit = Conduit.Create(
|
||||
Document,
|
||||
conduitType.Id,
|
||||
mepCurve.GetCurve().GetEndPoint(0),
|
||||
mepCurve.GetCurve().GetEndPoint(1),
|
||||
mepCurve.LevelId
|
||||
);
|
||||
firstConduits.Add(conduit);
|
||||
}
|
||||
}
|
||||
dictionary.Add(0, firstConduits);
|
||||
if (count > 2)
|
||||
{
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
List<Conduit> conduits = new();
|
||||
var x = Math.Pow(-1, i);
|
||||
var y = Math.Ceiling(i / 2.0);
|
||||
var offest = x * y * interval;
|
||||
if (count % 2 == 0)
|
||||
{
|
||||
offest += interval / 2;
|
||||
}
|
||||
|
||||
foreach (var conduit in firstConduits)
|
||||
{
|
||||
var line = conduit.GetCurve() as Line;
|
||||
var direction = line!.Direction.CrossProduct(XYZ.BasisZ);
|
||||
var id = ElementTransformUtils.CopyElement(Document, conduit.Id, direction * offest).FirstOrDefault();
|
||||
var conduitCopied = Document.GetElement(id) as Conduit;
|
||||
conduits.Add(conduitCopied);
|
||||
}
|
||||
dictionary.Add(i + 1, conduits);
|
||||
}
|
||||
Document.Regenerate();
|
||||
}
|
||||
for (var i = 0; i < dictionary.Count; i++)
|
||||
{
|
||||
var conduits = dictionary[i];
|
||||
for (var j = 0; j < conduits.Count - 1; j++)
|
||||
{
|
||||
var conduit = conduits[j];
|
||||
var conduit1 = conduits[j + 1];
|
||||
|
||||
var list = ConnectorAssist.GetNearestConnectors(conduit, conduit1);
|
||||
if (list[0] != null && list[1] != null)
|
||||
{
|
||||
Document.Create.NewElbowFitting(list[0], list[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count % 2 == 0)
|
||||
{
|
||||
Document.Delete(firstConduits.Select(e => e.Id).ToList());
|
||||
}
|
||||
},
|
||||
"敷设电缆"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user