開洞信息數據:
start
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
cmdOpening : IExternalCommand
{
Result Execute(ExternalCommandData cmdData, msg, ElementSet elements)
{
UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Selection sel = uiDoc.Selection;
Transaction ts = Transaction(doc, );
ts.Start();
strPath = ;
XmlDocument xmlDoc = XmlDocument();
xmlDoc.Load(strPath);
(XmlNode node xmlDoc.DocumentElement.ChildNodes)
{
Wall wall = doc.get_Element( ElementId(Convert.ToInt32(node.Attributes[].Value))) Wall;
XmlNode nodePt1 = node.SelectSingleNode();
XYZ pt1 = XYZ(.Parse(nodePt1.Attributes[].Value), .Parse(nodePt1.Attributes[].Value), .Parse(nodePt1.Attributes[].Value));
XmlNode nodePt2 = node.SelectSingleNode();
XYZ pt2 = XYZ(.Parse(nodePt2.Attributes[].Value), .Parse(nodePt2.Attributes[].Value), .Parse(nodePt2.Attributes[].Value));
doc.Create.NewOpening(wall, pt1, pt2);
}
ts.Commit();
Result.Succeeded;
}
}
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
cmd : IExternalCommand
{
List<Wall> FindDuctWall(Document doc, Duct duct)
{
List<Wall> listWall = List<Wall>();
BoundingBoxXYZ bb = duct.get_BoundingBox(doc.ActiveView);
Outline outline = Outline(bb.Min, bb.Max);
FilteredElementCollector collector = FilteredElementCollector(doc);
BoundingBoxIntersectsFilter invertFilter = BoundingBoxIntersectsFilter(outline, );
IList<Element> noIntersectWalls = collector.OfClass((Wall)).WherePasses(invertFilter).ToElements();
(Element el noIntersectWalls)
{
Wall wall = el Wall;
(wall != )
listWall.Add(wall);
}
listWall;
}
Result Execute(ExternalCommandData cmdData, msg, ElementSet elements)
{
UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
UIApplication uiApp = cmdData.Application;
Document doc = uiDoc.Document;
Selection selection = uiDoc.Selection;
{
Transaction ts = Transaction(doc, );
ts.Start();
strPath = ;
XmlDocument xmlDoc = XmlDocument();
xmlDoc.Load(strPath);
List<> listPath = GetLinkFilePaths(doc);
Document docLink = uiApp.Application.OpenDocumentFile(listPath[]);
FilteredElementCollector collIns = FilteredElementCollector(doc); collIns.OfClass((Instance)).OfCategory(BuiltInCategory.OST_RvtLinks); Transform transForm = ;
(Element elDoc collIns)
{
Instance ins = elDoc Instance;
(ins != )
{
transForm = ins.GetTransform();
}
}
FilteredElementCollector collDuct = FilteredElementCollector(doc);
collDuct.OfClass((Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);
(Element elDuct collDuct)
{
Duct duct = elDuct Duct;
List<Wall> listWall = FindDuctWall(docLink, duct); (Wall wall listWall)
{
Face face = FindWallFace(wall);
Curve curve = FindDuctCurve(duct);
XYZ xyz = FindFaceCurve(face, curve);
XYZ wallVector = FindWallVector(wall);
dWidth = GetDuctWidth(duct);
dHeigh = GetDuctHeight(duct);
iUp = ;
iDown = ;
iLeft = ;
iRight = ;
XYZ pt1 = xyz + XYZ(, , ) * (dHeigh / + iUp) / ;
pt1 = pt1 - wallVector.Normalize() * (dWidth / + iLeft) / ;
pt1 = pt1 - transForm.Origin;
XYZ pt2 = xyz + XYZ(, , -) * (dHeigh / + iDown) / ;
pt2 = pt2 + wallVector.Normalize() * (dWidth / + iRight) / ;
pt2 = pt2 - transForm.Origin;
XmlElement xmlEl = xmlDoc.CreateElement();
xmlEl.SetAttribute(, wall.Id.IntegerValue.ToString());
XmlElement elPt1 = xmlDoc.CreateElement();
elPt1.SetAttribute(, pt1.X.ToString());
elPt1.SetAttribute(, pt1.Y.ToString());
elPt1.SetAttribute(, pt1.Z.ToString());
xmlEl.AppendChild(elPt1);
XmlElement elPt2 = xmlDoc.CreateElement();
elPt2.SetAttribute(, pt2.X.ToString());
elPt2.SetAttribute(, pt2.Y.ToString());
elPt2.SetAttribute(, pt2.Z.ToString());
xmlEl.AppendChild(elPt2);
xmlDoc.DocumentElement.AppendChild(xmlEl);
}
}
ts.Commit();
xmlDoc.Save(strPath);
}
(Exception ex)
{
WinFormTools.MsgBox(ex.ToString());
}
Result.Succeeded;
}
GetDuctWidth(Duct duct)
{
dWidth = ;
(Parameter p duct.Parameters)
{
(p.Definition.Name == )
{
dWidth = .Parse(p.AsValueString());
;
}
}
dWidth;
}
GetDuctHeight(Duct duct)
{
dHeigh = ;
(Parameter p duct.Parameters)
{
(p.Definition.Name == )
{
dHeigh = .Parse(p.AsValueString());
;
}
}
dHeigh;
}
XYZ FindWallVector(Wall wall)
{
LocationCurve lCurve = wall.Location LocationCurve;
XYZ xyz = lCurve.Curve.get_EndPoint() - lCurve.Curve.get_EndPoint();
xyz;
}
XYZ FindFaceCurve(Face face, Curve curve)
{
IntersectionResultArray intersectionR = IntersectionResultArray(); SetComparisonResult comparisonR; comparisonR = face.Intersect(curve, intersectionR);
XYZ intersectionResult = ; (SetComparisonResult.Disjoint != comparisonR) {
(!intersectionR.IsEmpty)
{
intersectionResult = intersectionR.get_Item().XYZPoint;
}
}
intersectionResult;
}
Curve FindDuctCurve(Duct duct)
{
IList<XYZ> list = List<XYZ>();
ConnectorSetIterator csi = duct.ConnectorManager.Connectors.ForwardIterator();
(csi.MoveNext())
{
Connector conn = csi.Current Connector;
list.Add(conn.Origin);
}
Curve curve = Line.get_Bound(list.ElementAt(), list.ElementAt()) Curve;
curve.MakeUnbound();
curve;
}
Face FindWallFace(Wall wall)
{
Face normalFace = ;
Options opt = Options();
opt.ComputeReferences = ;
opt.DetailLevel = Autodesk.Revit.DB.DetailLevels.Medium;
GeometryElement e = wall.get_Geometry(opt);
IEnumerator<GeometryObject> enm = e.GetEnumerator();
while (enm.MoveNext())
{
Solid solid = enm.Current as Solid;
}
(GeometryObject obj e.Objects) {
Solid solid = obj Solid;
(solid != && solid.Faces.Size > )
{
(Face face solid.Faces)
{
PlanarFace pf = face PlanarFace;
(pf != )
{
(pf.Normal.AngleTo(wall.Orientation) < ) {
normalFace = face;
}
}
}
}
}
normalFace;
}
List<> GetLinkFilePaths(Document doc)
{
List<> listPath = List<>();
(ElementId elId ExternalFileUtils.GetAllExternalFileReferences(doc))
{
(doc.get_Element(elId).IsExternalFileReference())
{
ExternalFileReference fileRef = doc.get_Element(elId).GetExternalFileReference();
(ExternalFileReferenceType.RevitLink == fileRef.ExternalFileReferenceType)
listPath.Add(ModelPathUtils.ConvertModelPathToUserVisiblePath(fileRef.GetAbsolutePath()));
}
}
listPath;
}
GetPath(List<> listPath, strKey)
{
( strPath listPath)
{
(strPath.Contains(strKey))
strPath;
}
;
}
}