/***************************************************/ /**** Public methods ****/ /***************************************************/ public static View View(this DraftingInstance draftingInstance, Document document) { if (string.IsNullOrWhiteSpace(draftingInstance.ViewName)) { return(null); } List <View> views = new FilteredElementCollector(document).OfClass(typeof(View)).Cast <View>().ToList(); views.RemoveAll(x => x.IsTemplate || x is ViewSchedule || x is View3D || x is ViewSheet); View view = null; if (views != null && views.Count > 0) { view = views.Find(x => x.Name == draftingInstance.ViewName); } if (view != null) { return(view); } views = new FilteredElementCollector(document).OfClass(typeof(ViewSheet)).Cast <View>().ToList(); string title = draftingInstance.ViewName; if (!title.StartsWith("Sheet: ")) { title = string.Format("Sheet: {0}", title); } view = views.Find(x => x.Title == title); return(view); }
/***************************************************/ public static CurveElement ToCurveElement(this DraftingInstance draftingInstance, Document document, RevitSettings settings = null, Dictionary <Guid, List <int> > refObjects = null) { CurveElement curveElement = refObjects.GetValue <CurveElement>(document, draftingInstance.BHoM_Guid); if (curveElement != null) { return(curveElement); } settings = settings.DefaultIfNull(); if (!(draftingInstance.Location is ICurve)) { return(null); } ICurve curve = (ICurve)draftingInstance.Location; Curve revitCurve = curve.IToRevit(); if (revitCurve == null) { return(null); } if (!BH.Engine.Geometry.Query.IsPlanar(curve as dynamic)) { return(null); } View view = Query.View(draftingInstance, document); if (view == null) { return(null); } curveElement = document.Create.NewDetailCurve(view, revitCurve); if (curveElement == null) { return(null); } if (draftingInstance.Properties != null) { string name = draftingInstance.Properties.Name; if (!string.IsNullOrEmpty(name)) { Element element = new FilteredElementCollector(document).OfClass(typeof(GraphicsStyle)).ToList().Find(x => x.Name == name); if (element != null) { curveElement.LineStyle = element; } } } refObjects.AddOrReplace(draftingInstance, curveElement); return(curveElement); }
/***************************************************/ /**** Public Methods ****/ /***************************************************/ public static DraftingInstance DraftingInstanceFromRevit(this FilledRegion filledRegion, RevitSettings settings = null, Dictionary <string, List <IBHoMObject> > refObjects = null) { settings = settings.DefaultIfNull(); DraftingInstance draftingInstance = refObjects.GetValue <DraftingInstance>(filledRegion.Id); if (draftingInstance != null) { return(draftingInstance); } View view = filledRegion.Document.GetElement(filledRegion.OwnerViewId) as View; if (view == null) { return(null); } InstanceProperties instanceProperties = (filledRegion.Document.GetElement(filledRegion.GetTypeId()) as ElementType).InstancePropertiesFromRevit(settings, refObjects) as InstanceProperties; List <ICurve> curves = new List <oM.Geometry.ICurve>(); foreach (CurveLoop loop in filledRegion.GetBoundaries()) { curves.Add(loop.FromRevit()); } List <PlanarSurface> surfaces = BH.Engine.Geometry.Create.PlanarSurface(curves); if (surfaces.Count == 1) { draftingInstance = new DraftingInstance { Properties = instanceProperties, ViewName = view.Name, Location = surfaces[0] } } ; else { draftingInstance = new DraftingInstance { Properties = instanceProperties, ViewName = view.Name, Location = new PolySurface { Surfaces = surfaces.Cast <ISurface>().ToList() } } }; draftingInstance.Name = filledRegion.Name; //Set identifiers, parameters & custom data draftingInstance.SetIdentifiers(filledRegion); draftingInstance.CopyParameters(filledRegion, settings.ParameterSettings); draftingInstance.SetProperties(filledRegion, settings.ParameterSettings); refObjects.AddOrReplace(filledRegion.Id, draftingInstance); return(draftingInstance); } /***************************************************/ }
/***************************************************/ /**** Private Methods - Drafting Instances ****/ /***************************************************/ private static Element IToRevitElement(this DraftingInstance draftingInstance, ElementType elementType, View view, RevitSettings settings) { if (elementType == null) { Compute.ElementTypeNotFoundWarning(draftingInstance); return(null); } return(ToRevitElement(draftingInstance, elementType as dynamic, view, settings)); }
/***************************************************/ public static Element ToRevit(this DraftingInstance draftingInstance, Document document, RevitSettings settings = null, Dictionary <Guid, List <int> > refObjects = null) { switch (draftingInstance.BuiltInCategory(document)) { case BuiltInCategory.OST_Lines: return(draftingInstance.ToCurveElement(document, settings, refObjects)); default: return(draftingInstance.ToRevitElement(document, settings, refObjects)); } }
public static DraftingInstance DraftingInstance(InstanceProperties properties, string viewName, ISurface location) { if (properties == null || string.IsNullOrWhiteSpace(viewName)) { return(null); } List <PlanarSurface> surfaces = new List <PlanarSurface>(); if (location is PlanarSurface) { surfaces.Add((PlanarSurface)location); } else if (location is PolySurface) { PolySurface polySurface = (PolySurface)location; if (polySurface.Surfaces.Any(x => !(x is PlanarSurface))) { BH.Engine.Reflection.Compute.RecordError("Only PlanarSurfaces and PolySurfaces consisting of PlanarSurfaces can be used as location for ISurface-based DraftingInstances."); return(null); } surfaces = polySurface.Surfaces.Cast <PlanarSurface>().ToList(); } else { BH.Engine.Reflection.Compute.RecordError("Only PlanarSurfaces and PolySurfaces consisting of PlanarSurfaces can be used as location for ISurface-based DraftingInstances."); return(null); } foreach (PlanarSurface surface in surfaces) { Vector normal = (surface).Normal(); if (normal == null || 1 - Math.Abs(normal.DotProduct(Vector.ZAxis)) > Tolerance.Angle) { BH.Engine.Reflection.Compute.RecordError("Normal of the surface or its components is not parallel to the global Z axis."); return(null); } } DraftingInstance draftingInstance = new DraftingInstance() { Properties = properties, Name = properties.Name, ViewName = viewName, Location = location }; return(draftingInstance); }
public static DraftingInstance DraftingInstance(InstanceProperties properties, string viewName, ICurve location) { if (properties == null || string.IsNullOrWhiteSpace(viewName) || location == null) { return(null); } DraftingInstance draftingInstance = new DraftingInstance() { Properties = properties, Name = properties.Name, ViewName = viewName, Location = location }; return(draftingInstance); }
/***************************************************/ private static Element ToRevitElement(this DraftingInstance draftingInstance, FilledRegionType regionType, View view, RevitSettings settings) { ISurface location = draftingInstance.Location as ISurface; List <PlanarSurface> surfaces = new List <PlanarSurface>(); if (location is PlanarSurface) { surfaces.Add((PlanarSurface)location); } else if (location is PolySurface) { PolySurface polySurface = (PolySurface)location; if (polySurface.Surfaces.Any(x => !(x is PlanarSurface))) { draftingInstance.InvalidRegionSurfaceError(); return(null); } surfaces = polySurface.Surfaces.Cast <PlanarSurface>().ToList(); } else { draftingInstance.InvalidRegionSurfaceError(); return(null); } List <CurveLoop> loops = new List <CurveLoop>(); foreach (PlanarSurface surface in surfaces) { foreach (ICurve curve in surface.Edges()) { loops.Add(curve.ToRevitCurveLoop()); } } if (loops.Count != 0) { return(FilledRegion.Create(view.Document, regionType.Id, view.Id, loops)); } return(null); }
/***************************************************/ public static Element ToRevitElement(this DraftingInstance draftingInstance, Document document, RevitSettings settings = null, Dictionary <Guid, List <int> > refObjects = null) { if (draftingInstance == null || string.IsNullOrWhiteSpace(draftingInstance.ViewName) || document == null) { return(null); } Element element = refObjects.GetValue <Element>(document, draftingInstance.BHoM_Guid); if (element != null) { return(element); } if (draftingInstance.Properties == null) { Compute.NullObjectPropertiesWarning(draftingInstance); return(null); } settings = settings.DefaultIfNull(); View view = draftingInstance.View(document); if (view == null) { return(null); } ElementType elementType = draftingInstance.Properties.ElementType(document, draftingInstance.BuiltInCategories(document), settings); element = draftingInstance.IToRevitElement(elementType, view, settings); if (element == null) { return(null); } // Copy parameters from BHoM object to Revit element element.CopyParameters(draftingInstance, settings); refObjects.AddOrReplace(draftingInstance, element); return(element); }
/***************************************************/ private static Element ToRevitElement(this DraftingInstance draftingInstance, FamilySymbol familySymbol, View view, RevitSettings settings) { if (draftingInstance?.Location == null) { return(null); } if (draftingInstance.Location is ICurve) { return(Create.FamilyInstance(familySymbol.Document, familySymbol, ((ICurve)draftingInstance.Location).IToRevit(), view, settings)); } else if (draftingInstance.Location is BH.oM.Geometry.Point) { return(Create.FamilyInstance(familySymbol.Document, familySymbol, ((BH.oM.Geometry.Point)draftingInstance.Location).ToRevit(), draftingInstance.Orientation.ToRevit(), view, settings)); } else { return(null); } }
/***************************************************/ public static bool Update(this FamilyInstance element, DraftingInstance bHoMObject, RevitSettings settings, bool setLocationOnUpdate) { if (!element.ViewSpecific || element.OwnerViewId == null) { BH.Engine.Reflection.Compute.RecordError($"Updating model elements using DraftingInstances is not allowed. Revit ElementId: {element.Id} BHoM_Guid: {bHoMObject.BHoM_Guid}"); return(false); } if (!string.IsNullOrWhiteSpace(bHoMObject.Properties?.CategoryName) && element.Category?.Name != bHoMObject.Properties.CategoryName) { BH.Engine.Reflection.Compute.RecordWarning($"Updating the category of an existing Revit element is not allowed. Revit ElementId: {element.Id} BHoM_Guid: {bHoMObject.BHoM_Guid}"); } if (!string.IsNullOrWhiteSpace(bHoMObject.ViewName) && (element.Document.GetElement(element.OwnerViewId) as View)?.Name != bHoMObject.ViewName) { BH.Engine.Reflection.Compute.RecordWarning($"Updating the owner view of an existing Revit element is not allowed. Revit ElementId: {element.Id} BHoM_Guid: {bHoMObject.BHoM_Guid}"); } return(((Element)element).Update(bHoMObject, settings, setLocationOnUpdate)); }
public static DraftingInstance DraftingInstance(string viewName, ICurve location) { if (string.IsNullOrWhiteSpace(viewName) || location == null) { return(null); } InstanceProperties instanceProperties = new InstanceProperties(); instanceProperties.CategoryName = "Lines"; DraftingInstance draftingInstance = new DraftingInstance() { Properties = instanceProperties, Name = "Detail Lines", ViewName = viewName, Location = location }; return(draftingInstance); }
/***************************************************/ /**** Fallback Methods ****/ /***************************************************/ private static Element ToRevitElement(this DraftingInstance draftingInstance, ElementType elementType, View view, RevitSettings settings) { return(null); }
/***************************************************/ internal static void InvalidRegionSurfaceError(this DraftingInstance draftingInstance) { BH.Engine.Reflection.Compute.RecordError($"Only DraftingInstances with locations as PlanarSurfaces or PolySurfaces consisting of PlanarSurfaces can be converted into a FilledRegion. BHoM_Guid: {draftingInstance.BHoM_Guid}"); }