Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element type query centralised and made more logical #1154

Merged
merged 7 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Revit_Core_Engine/Convert/Physical/ToRevit/FamilyInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ public static FamilyInstance ToRevitFamilyInstance(this Column framingElement, D

Line columnLine = framingElement.Location.IToRevit() as Line;

FamilySymbol familySymbol = framingElement.Property.ToRevitElementType(document, framingElement.BuiltInCategories(document), settings, refObjects);
if (familySymbol == null)
familySymbol = framingElement.ElementType(document, settings) as FamilySymbol;

FamilySymbol familySymbol = framingElement.ElementType(document, settings);
if (familySymbol == null)
{
Compute.ElementTypeNotFoundWarning(framingElement);
Expand Down Expand Up @@ -203,13 +200,7 @@ public static FamilyInstance ToRevitFamilyInstance(this IFramingElement framingE

Level level = document.LevelBelow(framingElement.Location, settings);

// Always taking StructuralFraming category because most families belong to it.
HashSet<BuiltInCategory> categories = framingElement.BuiltInCategories(document);
categories.Add(BuiltInCategory.OST_StructuralFraming);
FamilySymbol familySymbol = framingElement.Property.ToRevitElementType(document, categories, settings, refObjects);
if (familySymbol == null)
familySymbol = framingElement.ElementType(document, categories, settings) as FamilySymbol;

FamilySymbol familySymbol = framingElement.ElementType(document, settings);
if (familySymbol == null)
{
Compute.ElementTypeNotFoundWarning(framingElement);
Expand Down
5 changes: 1 addition & 4 deletions Revit_Core_Engine/Convert/Physical/ToRevit/Floor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public static Floor ToRevitFloor(this oM.Physical.Elements.Floor floor, Document

settings = settings.DefaultIfNull();

FloorType floorType = floor.Construction?.ToRevitElementType(document, new List<BuiltInCategory> { BuiltInCategory.OST_Floors, BuiltInCategory.OST_StructuralFoundation }, settings, refObjects) as FloorType;
if (floorType == null)
floorType = floor.ElementType(document, settings);

FloorType floorType = floor.ElementType(document, settings);
if (floorType == null)
{
Compute.ElementTypeNotFoundWarning(floor);
Expand Down
5 changes: 1 addition & 4 deletions Revit_Core_Engine/Convert/Physical/ToRevit/RoofBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public static RoofBase ToRevitRoofBase(this oM.Physical.Elements.Roof roof, Docu

settings = settings.DefaultIfNull();

RoofType roofType = roof.Construction?.ToRevitElementType(document, new List<BuiltInCategory> { BuiltInCategory.OST_Roofs }, settings, refObjects) as RoofType;
if (roofType == null)
roofType = roof.ElementType(document, settings);

RoofType roofType = roof.ElementType(document, settings);
if (roofType == null)
{
Compute.ElementTypeNotFoundWarning(roof);
Expand Down
5 changes: 1 addition & 4 deletions Revit_Core_Engine/Convert/Physical/ToRevit/Wall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public static Wall ToRevitWall(this oM.Physical.Elements.Wall wall, Document doc

settings = settings.DefaultIfNull();

WallType wallType = wall.Construction.ToRevitElementType(document, new List<BuiltInCategory> { BuiltInCategory.OST_Walls }, settings, refObjects) as WallType;
if (wallType == null)
wallType = wall.ElementType(document,settings);

WallType wallType = wall.ElementType(document, settings);
if (wallType == null)
{
Compute.ElementTypeNotFoundWarning(wall);
Expand Down
4 changes: 2 additions & 2 deletions Revit_Core_Engine/Convert/Revit/ToRevit/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static Element ToRevitElement(this ModelInstance modelInstance, Document
}
else
{
ElementType elementType = modelInstance.Properties.ElementType(document, new List<BuiltInCategory> { builtInCategory }, settings);
ElementType elementType = modelInstance.ElementType(document, settings);
element = modelInstance.IToRevitElement(elementType, settings);
}

Expand Down Expand Up @@ -123,7 +123,7 @@ public static Element ToRevitElement(this DraftingInstance draftingInstance, Doc
if (view == null)
return null;

ElementType elementType = draftingInstance.Properties.ElementType(document, draftingInstance.BuiltInCategories(document), settings);
ElementType elementType = draftingInstance.ElementType(document, settings);
element = draftingInstance.IToRevitElement(elementType, view, settings);

if (element == null)
Expand Down
17 changes: 3 additions & 14 deletions Revit_Core_Engine/Modify/SetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ public static bool SetType(this FamilyInstance element, IFramingElement bHoMObje
if (element.TrySetTypeFromString(bHoMObject, settings))
return true;

Document doc = element.Document;
FamilySymbol familySymbol = bHoMObject.Property.ToRevitElementType(doc, bHoMObject.BuiltInCategories(doc), settings);
if (familySymbol == null)
familySymbol = bHoMObject.IElementType(doc, settings) as FamilySymbol;

FamilySymbol familySymbol = bHoMObject.ElementType(element.Document, settings);
if (familySymbol == null)
{
Compute.ElementTypeNotFoundWarning(bHoMObject);
Expand All @@ -75,11 +71,7 @@ public static bool SetType(this HostObject element, ISurface bHoMObject, RevitSe
if (element.TrySetTypeFromString(bHoMObject, settings))
return true;

Document doc = element.Document;
HostObjAttributes hostObjAttr = bHoMObject.Construction.ToRevitElementType(doc, bHoMObject.BuiltInCategories(doc), settings);
if (hostObjAttr == null)
hostObjAttr = bHoMObject.IElementType(doc, settings) as HostObjAttributes;

HostObjAttributes hostObjAttr = bHoMObject.IElementType(element.Document, settings) as HostObjAttributes;
if (hostObjAttr != null && hostObjAttr.Id.IntegerValue != element.GetTypeId().IntegerValue)
return element.SetParameter(BuiltInParameter.ELEM_TYPE_PARAM, hostObjAttr.Id);

Expand All @@ -98,10 +90,7 @@ public static bool SetType(this Element element, IInstance instance, RevitSettin
if (element.TrySetTypeFromString(instance, settings))
return true;

ElementType elementType = instance.Properties.ElementType(element.Document, settings);
if (elementType == null)
elementType = instance.IElementType(element.Document, settings);

ElementType elementType = instance.ElementType(element.Document, settings);
if (elementType != null)
{
try
Expand Down
19 changes: 19 additions & 0 deletions Revit_Core_Engine/Query/BuiltInCategories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,25 @@ public static HashSet<BuiltInCategory> BuiltInCategories(this Type bHoMType)
{
Autodesk.Revit.DB.BuiltInCategory.OST_GenericModel,
}
},
{
typeof (BH.oM.Physical.Elements.Wall), new BuiltInCategory[]
{
Autodesk.Revit.DB.BuiltInCategory.OST_Walls,
}
},
{
typeof (BH.oM.Physical.Elements.Floor), new BuiltInCategory[]
{
Autodesk.Revit.DB.BuiltInCategory.OST_Floors,
Autodesk.Revit.DB.BuiltInCategory.OST_StructuralFoundation,
}
},
{
typeof (BH.oM.Physical.Elements.Roof), new BuiltInCategory[]
{
Autodesk.Revit.DB.BuiltInCategory.OST_Roofs,
}
}
};

Expand Down
83 changes: 78 additions & 5 deletions Revit_Core_Engine/Query/ElementType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using BH.oM.Adapters.Revit.Settings;
using BH.oM.Base;
using BH.oM.Base.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand Down Expand Up @@ -53,14 +54,44 @@ public static ElementType IElementType(this IBHoMObject bHoMObject, Document doc
/**** Public methods ****/
/***************************************************/

[Description("Returns the Revit element type to be used when converting a given BHoM framing element to Revit.")]
[Input("framingElement", "BHoM framing element to find a correspondent Revit element type for.")]
[Input("document", "Revit document to parse in search for the element type.")]
[Input("settings", "Revit adapter settings to be used while performing the query.")]
[Output("framingType", "Revit element type to be used when converting the input BHoM object to Revit.")]
public static FamilySymbol ElementType(this BH.oM.Physical.Elements.IFramingElement framingElement, Document document, RevitSettings settings = null)
{
HashSet<BuiltInCategory> categories = framingElement.BuiltInCategories(document);

// Taking StructuralFraming category when not a column because most families belong to it.
if (framingElement is BH.oM.Physical.Elements.Column)
categories.Add(Autodesk.Revit.DB.BuiltInCategory.OST_StructuralFraming);

return framingElement.ElementTypeInclProperty(framingElement.Property, document, categories, settings) as FamilySymbol;
}

/***************************************************/

[Description("Returns the Revit element type to be used when converting a given BHoM IInstance to Revit.")]
[Input("instance", "BHoM IInstance to find a correspondent Revit element type for.")]
[Input("document", "Revit document to parse in search for the element type.")]
[Input("settings", "Revit adapter settings to be used while performing the query.")]
[Output("elementType", "Revit element type to be used when converting the input BHoM object to Revit.")]
public static ElementType ElementType(this BH.oM.Adapters.Revit.Elements.IInstance instance, Document document, RevitSettings settings = null)
{
return instance.ElementTypeInclProperty(instance.Properties, document, new HashSet<BuiltInCategory> { instance.BuiltInCategory(document) }, settings);
}

/***************************************************/

[Description("Returns the Revit wall type to be used when converting a given BHoM wall to Revit.")]
[Input("wall", "BHoM wall to find a correspondent Revit element type for.")]
[Input("document", "Revit document to parse in search for the element type.")]
[Input("settings", "Revit adapter settings to be used while performing the query.")]
[Output("wallType", "Revit wall type to be used when converting the input BHoM object to Revit.")]
public static WallType ElementType(this BH.oM.Physical.Elements.Wall wall, Document document, RevitSettings settings = null)
{
return wall.ElementType<WallType>(document);
return wall.ElementTypeInclProperty<WallType>(document, settings);
}

/***************************************************/
Expand All @@ -72,7 +103,7 @@ public static WallType ElementType(this BH.oM.Physical.Elements.Wall wall, Docum
[Output("floorType", "Revit floor type to be used when converting the input BHoM object to Revit.")]
public static FloorType ElementType(this BH.oM.Physical.Elements.Floor floor, Document document, RevitSettings settings = null)
{
return floor.ElementType<FloorType>(document);
return floor.ElementTypeInclProperty<FloorType>(document, settings);
}

/***************************************************/
Expand All @@ -84,7 +115,7 @@ public static FloorType ElementType(this BH.oM.Physical.Elements.Floor floor, Do
[Output("roofType", "Revit roof type to be used when converting the input BHoM object to Revit.")]
public static RoofType ElementType(this BH.oM.Physical.Elements.Roof roof, Document document, RevitSettings settings = null)
{
return roof.ElementType<RoofType>(document);
return roof.ElementTypeInclProperty<RoofType>(document, settings);
}

/***************************************************/
Expand Down Expand Up @@ -236,8 +267,50 @@ private static void FamilyAndTypeNames(this IBHoMObject bHoMObject, out string f
}

/***************************************************/
}
}

private static T ElementTypeInclProperty<T>(this BH.oM.Physical.Elements.ISurface surface, Document document, RevitSettings settings = null) where T : HostObjAttributes
{
Type bhomType = surface?.GetType();
if (bhomType == null)
return null;

bool differentNames = !string.IsNullOrWhiteSpace(surface.Construction?.Name) && !string.IsNullOrWhiteSpace(surface.Name) && surface.Construction.Name != surface.Name;
T elementType = surface.Construction.ElementType(document, bhomType.BuiltInCategories(), settings) as T;
if (elementType == null)
{
elementType = surface.ElementType<T>(document);
if (elementType != null && differentNames)
BH.Engine.Base.Compute.RecordWarning($"BHoM {bhomType.Name}'s name and its {nameof(BH.oM.Physical.Elements.ISurface.Construction)} name are different. Revit element type has been set based on the former because no type has been found for the latter. BHoM_Guid: {surface.BHoM_Guid}");
}
else if (differentNames)
BH.Engine.Base.Compute.RecordWarning($"BHoM {bhomType.Name}'s name and its {nameof(BH.oM.Physical.Elements.ISurface.Construction)} name are different. Revit element type has been set based on the latter as it is meant to take precedence. BHoM_Guid: {surface.BHoM_Guid}");

return elementType;
}

/***************************************************/

private static ElementType ElementTypeInclProperty(this IBHoMObject bhomObject, IBHoMObject property, Document document, IEnumerable<BuiltInCategory> categories, RevitSettings settings = null)
{
Type bhomType = bhomObject?.GetType();
if (bhomType == null)
return null;

bool differentNames = !string.IsNullOrWhiteSpace(property?.Name) && !string.IsNullOrWhiteSpace(bhomObject.Name) && property.Name != bhomObject.Name;
ElementType elementType = property.ElementType(document, categories, settings);
if (elementType == null)
{
elementType = bhomObject.ElementType(document, categories, settings);
if (elementType != null && differentNames)
BH.Engine.Base.Compute.RecordWarning($"BHoM {bhomType.Name}'s name and its {nameof(BH.oM.Physical.Elements.ISurface.Construction)} name are different. Revit element type has been set based on the former because no type has been found for the latter. BHoM_Guid: {bhomObject.BHoM_Guid}");
}
else if (differentNames)
BH.Engine.Base.Compute.RecordWarning($"BHoM {bhomType.Name}'s name and its {nameof(BH.oM.Physical.Elements.ISurface.Construction)} name are different. Revit element type has been set based on the latter as it is meant to take precedence. BHoM_Guid: {bhomObject.BHoM_Guid}");

return elementType;
}

/***************************************************/
}
}