Himmelreich Kevin book-Manuel Python for Revit

Hi,
I have an error with the script page 176 of the book, in the filters by boolean operation. In the note it is specified that you must modify the subroutine (def) “solidoCampVisual”
Does anyone have any ideas?

I can pass the codes if I need to.
Daniel OLIVES
Lyon-FRANCE

Contact the author I’d say, or share the error in more detail. Most of us dont have the book, and if an author wants to lock away knowledge in a paid resource it is on them to troubleshoot with its customers imo.

2 Likes

Hi GavinCrump,
Thank’s for your answer.

Here is the code:

import clr
import sys
#sys.path.append('C:\Program Files\IronPython 2.7\Lib')
#2022/12
sys.path.append('C:\Program Files\IronPython 3.4\Lib')
import System
from System import Array
from System.Collections.Generic import *

import traceback

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List; BuiltInCategory
# - - - - M.E.P.- - - - - - - - - -
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Mechanical import *
# - - - - - - - - - - - - - - - - - 
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument
# - - - - - - FONCTIONS - - - - - - -
def toList(x):
    if isinstance(x, list):
        return x
    elif hasattr(x, "GetType") and x.GetType().GetInterface("IEnumerable") is not None:
        return x
    else :
        return [x]
def solidVisualField(vue):
	crMngr = vue.GetCropRegionShapeManager()
	vrange = vue.GetViewRange()
	desf = vrange.GetOffset(PlanViewPlane.TopClipPlane) - vrange.GetOffset(PlanViewPlane.ViewDepthPlane)
	CropRegion = crMngr.GetCropShape()
	lignes = CurveLoop()
	[lignes.Append(x) for x in CropRegion[0]]
	L = List[CurveLoop]()
	L.Add(lignes)
	return GeometryCreationUtilities.CreateExtrusionGeometry(L,XYZ(0,0,1),desf)      

def RvtGeoADirectShape(solide) :
	#Créer un DirectShape à travers un solide, toujours exécuté à l'intérieur d'une opération
	listGeo = List[GeometryObject]()
	listGeo.Add(solide)
	catégorie = ElementId(BuiltInCategory.OST_GenericModel)
	ShapeRvt = DirectShape.CreateElement(doc, catégorie)
	ShapeRvt.AppendShape(listGeo)
	return ShapeRvt
	
def LnkElemsVisibleInView(link, campoVisual):
	"""Le champ visuel est représenté par le solide extrudé qui est créé à partir des lignes de la région de découpe de la vue"""
	transf = link.GetTransform()
	if not transf.AlmostEqual(Transform.Identity):
		campoVisual=SolidUtils.CreateTransformed(campoVisual,transf.Inverse)
	filtro = ElementIntersectsSolidFilter(campoVisual)
	elems = FilteredElementCollector(link.GetLinkDocument()).WherePasses(filtro).ToElements()
	return elems

def solidoHabsLinks(habs):
	salida = []
	for h in habs:
		geo = list(h.ClosedShell)
		salida.append(geo[0])
	return salida

def LnkhabsEnVista(link, vue):
	pièces = FilteredElementCollector(link.GetLinkDocument()).OfCategory(BuiltInCategory.OST_Rooms).ToElements()
	areaMin = min([x.Area for x in pièces])
	solide = solidVisualField(vue)
	campoVisual = solide[0]
	desf = solide[1]
	habGeo = solidoHabsLinks(pièces)
	transf = link.GetTransform()
	if not transf.AlmostEqual(Transform.Identity):
		campoVisual = SolidUtils.CreateTransformed(campoVisual,transf.Inverse)
	habEnVista = []
	for h,s in zip(pièces, habGeo):
		intersc = BooleanOperationsUtils.ExecuteBooleanOperation(campoVisual, s, BooleanOperationsType.Intersect)
		area = intersc.Volume / desf
		if area >= areaMin:
			habEnVista.append(h)
		return habEnVista
# - - - - - - - - - - - - - - - - - -
#Préparation des "entrées" de dynamo pour revit
vue = UnwrapElement(IN[0])
# - - - - - - - - - - - - - - - - - -
# - - - - - À PARTIR DE MAINTENANT LE CODE - - - - - -
solide = solidVisualField(vue)
#Faire quelque chose dans la Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
# - - - - - - - - - - - - - - - - - -
links= FilteredElementCollector (doc, vue.Id).OfClass(RevitLinkInstance)

pièces = [LnkhabsEnVista(x, vue) for x in links] 
	
#- - - - - - - - - - - - - - - - - - - - - - - - - - -
TransactionManager.Instance.TransactionTaskDone()
#- - - - - - - - - - - - - - - - - - - - - - - - - - -
OUT = pièces

And the note added:

Note: The solidVisualField function has been modified by adding as output the prism offset in the index 1, this will allow to calculate the area of the intersection and discard rooms that are partially seen in the view.

Daniel OLIVES
Lyon-FRANCE

Personally the language in the code and the goals of the script aren’t overly clear to me unfortunately. Hopefully the author can assist or someone more familiar with the language or book can help.

1 Like

Thank’s Gavin, i try to contact Kevin
Daniel