Skip to content

Commit

Permalink
Merge pull request #1857 from thumDer/feature/get-centroid
Browse files Browse the repository at this point in the history
Issue #1830
  • Loading branch information
jmcouffin authored Jul 14, 2023
2 parents 6ddb928 + a814ee0 commit 96abf97
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: Get Centroid
tooltip: >
Calculates the centroid of the selected elements geometry
and places three model lines intersecting at the centroid.
author: Tamás Déri
context: selection
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from __future__ import print_function
from pyrevit import script, revit, DB, forms
from Autodesk.Revit.Exceptions import InvalidOperationException

doc = revit.doc
uidoc = revit.uidoc

logger = script.get_logger()
output = script.get_output()


def merge_solids(solids):
"""Merges the provided list of solids into a single unified solid
Args:
solids (list(DB.Solid)): The solids to merge
Raises:
ValueError: If the input is an empty list
Returns:
DB.Solid: The union of the solids
"""
union = None
if not solids:
raise ValueError('List of provided solids was empty.')
for solid in solids:
if not union:
union = solid
else:
try:
union = DB.BooleanOperationsUtils.ExecuteBooleanOperation(
union,
solid,
DB.BooleanOperationsType.Union
)
except InvalidOperationException:
ref = solid.Faces[0].Reference.ConvertToStableRepresentation(doc)
uid = ref.split(':')[0]
e = doc.GetElement(uid)
logger.error(
'Failed to merge solid from element {}, '
'it will be excluded from the calculation'.format(
revit.ElementWrapper(e).name
)
)
return union


selection = revit.get_selection()
if not selection:
forms.alert('You must select one element.', exitscript=True)
logger.debug('selection: {}'.format(selection))

extracted_solids = []

for element in selection:
logger.debug('processing {}'.format(element.Name))
extracted_solids.extend([
g for g in revit.query.get_geometry(element, compute_references=True)
if isinstance(g, DB.Solid) and g.Faces.Size > 0
])

logger.debug('extracted_solids: {}'.format(extracted_solids))

merged_solid = merge_solids(extracted_solids)
logger.debug('merged_solid: {}'.format(merged_solid))

centroid = merged_solid.ComputeCentroid()
logger.debug('centroid: {}'.format(centroid))

with revit.Transaction('Get Centroid'):
sp1 = DB.SketchPlane.Create(
doc,
DB.Plane.CreateByNormalAndOrigin(
DB.XYZ.BasisZ,
centroid
)
)
sp2 = DB.SketchPlane.Create(
doc,
DB.Plane.CreateByNormalAndOrigin(
DB.XYZ.BasisY,
centroid
)
)
c1 = DB.Line.CreateBound(
centroid - DB.XYZ.BasisX,
centroid + DB.XYZ.BasisX
)
c2 = DB.Line.CreateBound(
centroid - DB.XYZ.BasisY,
centroid + DB.XYZ.BasisY
)
c3 = DB.Line.CreateBound(
centroid - DB.XYZ.BasisZ,
centroid + DB.XYZ.BasisZ
)

doc.Create.NewModelCurve(
c1, sp1
)
doc.Create.NewModelCurve(
c2, sp1
)
doc.Create.NewModelCurve(
c3, sp2
)
3 changes: 2 additions & 1 deletion pyrevitlib/pyrevit/revit/db/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1571,9 +1571,10 @@ def get_element_workset(element):
return workset_table.GetWorkset(element.WorksetId)


def get_geometry(element, include_invisible=False):
def get_geometry(element, include_invisible=False, compute_references=False):
geom_opts = DB.Options()
geom_opts.IncludeNonVisibleObjects = include_invisible
geom_opts.ComputeReferences = compute_references
geom_objs = []
for gobj in element.Geometry[geom_opts]:
if isinstance(gobj, DB.GeometryInstance):
Expand Down

0 comments on commit 96abf97

Please sign in to comment.