Create dataframe of all the parameters of an instance

Hello,

How to get parameters from an instance… here my try based on the book beyond dynamo…

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

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

clr.AddReference('System')
from System.Collections.Generic import List

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

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

def parametersDataFrame(document,category):
	elems = FilteredElementCollector(document).OfCategory(category).WhereElementIsNotElementType()
	if list(elems):
		par = [x.Parameters for x in elems]
		params = [[x for x in y if x.AsElementId().IntegerValue != category] for y in par]
		paramNames = [[x.Definition.Name for x in y] for y in params]
		header = paramNames[0]
		d = {}
		for x in header:
			d[x] = list()
		for pg,ng in zip(params, paramNames):
			for p,n in zip(pg,ng):
				d[n].append(p)
		df = pd.DataFrame(d, columns = header)
		df["Instances"] = list(elems)
		df["Element Names"] = [x.Name for x in elems]
		df["Types"] = [doc.GetElement(x.GetTypeId()) for x in elems]
		df["Item"] = [doc.GetElement(x.GetTypeId()).get_Parameter(BuiltInParameter.UNIFORMAT_CODE) for x in elems]
		return df
	else:
		return "No instances of this category exist"

instance = UnwrapElement(IN[0])

snoop = parametersDataFrame(doc, instance)

OUT = snoop
		

which input do i need ?

KR

Andreas

The code is built to take a category as an input, not an element.

To just get the parameters from an element, use element.Parameters. The result will be a list, but if you take one item out and get the documentation, and members directory of the object you should have a good clue on where to go from there.

1 Like

@jacob.small ,


my scipt runs… there one variable, which i am not able to identify :frowning:

KR

Andreas

Can you post a screenshot of the code?

1 Like
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

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

clr.AddReference('System')
from System.Collections.Generic import List

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

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

def parametersDataFrame(document,category):
	elems = FilteredElementCollector(document).OfCategory(category).WhereElementIsNotElementType()
	if list(elems):
		par = [x.Parameters for x in elems]
		params = [[x for x in y if x.AsElementId().IntegerValue != category] for y in par]
		paramNames = [[x.Definition.Name for x in y] for y in params]
		header = paramNames[0]
		d = {}
		for x in header:
			d[x] = list()
		for pg,ng in zip(params, paramNames):
			for p,n in zip(pg,ng):
				d[n].append(p)
		df = pd.DataFrame(d, columns = header)
		df["Instances"] = list(elems)
		df["Element Names"] = [x.Name for x in elems]
		df["Types"] = [doc.GetElement(x.GetTypeId()) for x in elems]
		df["Item"] = [doc.GetElement(x.GetTypeId()).get_Parameter(BuiltInParameter.UNIFORMAT_CODE) for x in elems]
		return df
	else:
		return "No instances of this category exist"

cat = IN[0]

snoop = parametersDataFrame(doc, cat)

OUT = snoop
	

KR

Andreas

You are calling the variable pd, but it hasn’t been defined anywhere. What are you expecting the pd object to be?

1 Like

@jacob.small ,

thats the question

i think it is a syntex error in the book, the variable is part of the function(def).

is it global variabl ?
what does a “dataframe” do? … the call it “panda DataFrame”
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html
in the book it is part of the chapter 2.2. accessing familyparameters

@jacob.small ,

is this a way? or is the solution in my Code?

hmmm…

KR

Andreas

It may be that you need to import pandas (and likely numpy and perhaps others) using a particular alias (ie: pd).

1 Like

@jacob.small ,

i found it it is pandas ! how to import this library ?

KR

Andreas

You need to use CPython3 or IronPython3.

See this link for how to customize: Customizing Dynamo's Python 3 installation · DynamoDS/Dynamo Wiki · GitHub

2 Likes

Hello, you can look at this subject

and even more contributions on his blog dated June 25, thanks to Mr. Poupin.

Cordially
christian.stan

3 Likes

Hi,

if you use IronPython (2 or 3) it is better to use DataTables (Microsoft)

if you use CPython3 it is better to use DataFrame (Pandas)

In the future, IronPython3 should integrate the compatibility of packages written in C like numpy or pandas (via IronClad lib)

my last article about this, use the translation widget (thanks @christian.stan for mention)

2 Likes

@c.poupin @christian.stan @jacob.small ,

None of these codes works…

i think i will need some foldermanagement…



hi, it works, weird
you wouldn’t have made a mistake on the way
image

Python script
import clr
import sys
import os
localapp = os.getenv(r'LOCALAPPDATA')
sys.path.append(os.path.join(localapp, r'python-3.9.12-embed-amd64\Lib\site-packages'))
import numpy as np
import pandas as pd

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class Form_df(Form):
    def __init__(self, df):
        self.df = df
        self._html = df.to_html()
        self.InitializeComponent()
    
    def InitializeComponent(self):
        self._webBrowser1 = System.Windows.Forms.WebBrowser()
        self.SuspendLayout()
        # 
        # webBrowser1
        # 
        self._webBrowser1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right
        self._webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill
        self._webBrowser1.Location = System.Drawing.Point(0, 0)
        self._webBrowser1.MinimumSize = System.Drawing.Size(20, 20)
        self._webBrowser1.Name = "webBrowser1"
        self._webBrowser1.DocumentText  = self._html
        self._webBrowser1.Size = System.Drawing.Size(678, 378)
        self._webBrowser1.TabIndex = 0
        
        # 
        # Form_df
        # 
        self.ClientSize = System.Drawing.Size(678, 378)
        self.Controls.Add(self._webBrowser1)
        self.Name = "Form14"
        self.Text = "Modes constructifs et volume et Id"
        self.ResumeLayout(False)

all_walls = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()

data ={}

data["Modes constructifs"] = [x.LookupParameter("Commentaires").AsString() for x in all_walls]
data["Vol."] = [round(x.LookupParameter("Volume").AsDouble()*0.3048**3,3) for x in all_walls]
data["N°Id"] = [str(x.Id) for x in all_walls]

df = pd.DataFrame.from_dict(data)
a=Form_df(df)
a.Show()

OUT = repr(df),df.to_numpy()

cordially
christian.stan

1 Like

@christian.stan ,

i still struggle with cmd

get-pip.py file and locating my lib :frowning:

you followed the wiki posted by Mr. Jacob, you managed to install pip in Local and not roaming as seen on one of your captures
cordially
christian.stan

1 Like

@christian.stan ,

yes i did, i almost there

there are some parts different, the easy installationfiles are missing


the guide said i can ignore these errors…

try ...amd64>.\Scripts\pip install numpy

with point .

cordially
christian.stan

there is still trouble

have i to close revit ?

pip is defnitly here !!