Client/Desktop/KMBIM3.0/23.10.16/Cmd/PipeRack/LinkedFunction.cs

147 lines
5.6 KiB
C#

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace KMBIM
{
public class LinkedFunction
{
public IEnumerable<ExternalFileReference> GetLinkedFileReference(Autodesk.Revit.DB.Document doc)
{
var collector = new FilteredElementCollector(doc);
List<ExternalFileReference> linkedElements = collector
.OfClass(typeof(RevitLinkType))
.Select(x => x.GetExternalFileReference())
.ToList();
return linkedElements;
}
public IEnumerable<Autodesk.Revit.DB.Document> GetLinkedDocuments(Autodesk.Revit.DB.Document doc)
{
var linkedFiles = GetLinkedFileReference(doc);
var linkedFileNames = linkedFiles
.Select(x => ModelPathUtils
.ConvertModelPathToUserVisiblePath(
x.GetAbsolutePath())).ToList();
return doc.Application.Documents
.Cast<Autodesk.Revit.DB.Document>()
.Where(document => linkedFileNames.Any(
filename => document.PathName.Equals(filename)));
}
public void SelectFaceInLinkedFile(UIApplication uiapp, out PlanarFace pFace, out Reference pickRefer)
{
Document doc = uiapp.ActiveUIDocument.Document;
pFace = null;
pickRefer = null;
IEnumerable<Autodesk.Revit.DB.Document> doc2 = GetLinkedDocuments(doc);
Selection sel = uiapp.ActiveUIDocument.Selection;
try
{
bool loop = true;
while (loop)
{
pickRefer = sel.PickObject(ObjectType.PointOnElement, "면 선택 :");
Element elem = doc.GetElement(pickRefer.ElementId);
XYZ pos = pickRefer.GlobalPoint;
string s = pickRefer.ConvertToStableRepresentation(doc);
string[] tab_str = s.Split(':');
string id = tab_str[tab_str.Length - 3];
int ID;
Int32.TryParse(id, out ID);
Type et = elem.GetType();
if (typeof(RevitLinkType) == et || typeof(RevitLinkInstance) == et || typeof(Instance) == et)
{
foreach (Autodesk.Revit.DB.Document d in doc2)
{
if (elem.Name.Contains(d.Title))
{
Element element = d.GetElement(new ElementId(ID));
Options opts = new Options();
opts.ComputeReferences = true;
//MessageBox.Show(element.Name, element.get_Geometry(opts).ToString());
GeometryElement geoElem = element.get_Geometry(opts);
if (geoElem == null)
{
MessageBox.Show("선택한 객체가 면이 아닙니다.", "오류");
break;
}
GeometryObject obj = geoElem.First();
foreach (GeometryObject obj2 in element.get_Geometry(opts))
{
if (obj2 is Solid)
{
Solid solid2 = obj2 as Solid;
foreach (Face face2 in solid2.Faces)
{
try
{
if (face2.Project(pos).XYZPoint.DistanceTo(pos) == 0)
{
pFace = face2 as PlanarFace;
return;
}
}
catch
{
}
}
}
}
}
}
}
else//링크 인스턴스가 아닌경우
{
//선택한 면 Face로 변경
GeometryObject geoObject = doc.GetElement(pickRefer).GetGeometryObjectFromReference(pickRefer);
if (geoObject == null)
{
MessageBox.Show("선택한 객체가 면이 아닙니다.", "오류");
}
else
{
//Face rface = geoObject as Face;
pFace = geoObject as PlanarFace;
if (pFace == null)
MessageBox.Show("선택한 객체가 면이 아닙니다.", "오류");
else
return;
}
}
}
}
catch
{
}
return;
}
}
}