Reputation: 35
So when i get a bounding box of a door element in dynamo it gives me a bounding box including the door swing. EX:
and when i get the bounding box of a door geometry it gives me the bounding box excluding the door swing EX:
NOW USING C#!!!! so i thought the same logic would apply. I can get the bounding box of the element and it will include the doorswing.
but I want a bounding box just around the door geometry. I have been trying to find out why i cant do this with no luck. Here is the error im getting.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
namespace DynamoToRevitPlugin
{
[Transaction(TransactionMode.Manual)]
class RebarClearancesCMU : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
//need to select elements before
// Get the handle of current document
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
// reference the form
RebarClearanceCMUForm form = new RebarClearanceCMUForm(uidoc);
// Get the element selection of current document.
Selection selection = uidoc.Selection;
ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
int allDoors = 0;
int successDoors = 0;
int allWindows = 0;
int successWindows = 0;
//set family to null
Family family = null;
//declare path to family
String familyPath = "C:\\Users\\zrodgers\\Desktop\\Dev\\DynamoToRevitPlugin\\RebarClearanceFamily.rfa";
//declare empty string
string str = "";
//gets clearance input in inches
var clearance = (form.clearanceOffset.Value/12);
if (0 == selectedIds.Count)
{
// If no elements selected.
TaskDialog.Show("Revit", "Select one or more elements before running plugin.");
}
else
{
if (form.ShowDialog() == DialogResult.OK)
{
FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(GraphicsStyle));
GraphicsStyle style = collector.Cast<GraphicsStyle>().FirstOrDefault<GraphicsStyle>(gs => gs.Name.Equals("<Sketch>"));
ElementId categoryId = new ElementId(BuiltInCategory.OST_GenericModel);
using (Transaction tx = new Transaction(doc))
{
tx.Start("Place Family");
// load family
doc.LoadFamily(familyPath, out family);
//get family types from revit
FilteredElementCollector colEle = new FilteredElementCollector(doc);
colEle.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_GenericModel);
//grab first
FamilySymbol firstClearance = colEle.FirstElement() as FamilySymbol;
//activate family
if (!firstClearance.IsActive)
{
firstClearance.Activate();
}
foreach (ElementId elemId in selectedIds)
{
try
{
Element elem = uidoc.Document.GetElement(elemId);
if ((BuiltInCategory)elem.Category.Id.IntegerValue == BuiltInCategory.OST_Doors)
{
allDoors++;
Options opts = new Options();
opts.IncludeNonVisibleObjects = false;
BoundingBoxXYZ bbDoor = elem.get_BoundingBox(uidoc.Document.ActiveView);
LocationPoint doorCenter = elem.Location as LocationPoint;
GeometryElement geoElem = elem.get_Geometry(opts);
//get geometry object
foreach (GeometryObject geoObj in geoElem)
{
GeometryInstance geoInst = geoObj as GeometryInstance;
if(null !=geoInst)
{
GeometryElement instGeoElem = geoInst.GetInstanceGeometry();
if(instGeoElem != null)
{
foreach (GeometryObject o in instGeoElem)
{
//find solids
Solid solid = o as Solid;
BoundingBoxXYZ solidBB = solid.GetBoundingBox();
//gets center bottom of bounding box
XYZ doorMax = new XYZ(solidBB.Max.X, solidBB.Max.Y, solidBB.Min.Z);
XYZ bbDoorCenter = ((doorMax + solidBB.Min) / 2);
//sets family
doc.Create.NewFamilyInstance(bbDoorCenter, firstClearance, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
//Transform rotDoorTransform = Transform.CreateRotationAtPoint(XYZ.BasisZ, doorCenter.Rotation, bbDoorCenter);
}
}
}
}
}
else
{
if ((BuiltInCategory)elem.Category.Id.IntegerValue == BuiltInCategory.OST_Windows)
{
allWindows++;
BoundingBoxXYZ bbWindows = elem.get_BoundingBox(uidoc.Document.ActiveView);
LocationPoint windowCenter = elem.Location as LocationPoint;
doc.Create.NewFamilyInstance(windowCenter.Point, firstClearance, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
}
}
}
catch(Exception e)
{
message = e.Message;
}
}
tx.Commit();
}
}
}
return Result.Succeeded;
}
catch(Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
}
Upvotes: 3
Views: 1797
Reputation: 161
I agree with @Toni, the GeometryObject
might not be a Solid
, you can also use this syntax
if (o is Solid solid)
{
// Do Something
}
Upvotes: 3