How to Use Python Scripts in BricsCAD BIM
Quick answer: BIMPYTHON queries and manages data from a model within BricsCAD BIM using a .py Python script, anything from simple property lookups to elaborate calculations based on model parameters. BricsCAD doesn’t ship a Python Shell, so prepare scripts in a separate text or code editor. Python itself, along with its standard libraries, is embedded in BricsCAD BIM already, no separate install needed unless your script uses custom packages. Several sample scripts ship with the BricsCAD BIM installer.
Setting Up a Python Script
- Import whatever modules your script needs before accessing the API. Standard library modules use
import; external ones typically useimport as:
import math
import matplotlib.pyplot as plt
- Import
current_modelfrom thebricscad.bimmodule, your entry point for querying the model’s BIM objects:
from bricscad.bim import current_model
- Query the model. A few example patterns:
# Display info about the lengths of walls in the model
lengths = [wall.prop('Length') for wall in current_model().filter(Type='Wall')]
print(f'wall lengths. max: {max(lengths)}, avg: {sum(lengths)/len(lengths)}')
# Create a selection and print the objects
current_model().filter(Type='Wall', IsExternal=True).select()
for wall in bim_model.filter(Type='Wall', IsExternal=True, Length=max(lengths)):
print(wall)
bricscad.bim.Objects mappers are chainable, for example, finding parts of a roof close to a wall:
# Get all parts of roof within 40cm range of all walls
roof_parts = current_model().filter(Type='Roof').parts()
roof_parts_close_to_wall = current_model().filter(Type='Wall').within_distance(40, 'cm', search_range=roof_parts)
Or filter using a function statement:
# Filter roof parts longer than 50 project units
def is_long(obj):
return obj.prop('Length') > 50
roof_parts.filter(is_long)
Export or visualize data in various formats:
# create a dictionary list
wall_info = [
{ 'Handle': wall.get_property('Handle'),
'Length': wall.get_property('Length'),
'Height': wall.get_property('Height')
} for wall in current_model().filter(Type='Wall')]
# export to .json
import json
file = open('path/to/file.json', 'w+')
file.write(json.dumps(wall_info, indent=4))
file.close()
# plotting a histogram
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(wall_info)
df.hist();
plt.show()
# export to .csv
df.to_csv(r'path/to/file.csv', index = False, header=True)
Executing the Python Script
- Open a new or existing BricsCAD file where you want to run the script.
- Type
BIMPYTHONand press Enter. - In the dialog that appears, select a Python script file (
.py) and click Open to run it. - Unless your script exports or displays data externally, BricsCAD reports the output directly in the Command line.
API Reference
Object methods:
get_property(prop_name): returns the property value with the given name.set_property(prop_name, value): sets a value for that property.distance_to(other_obj, units='mm', distance_mode='exact'): calculates distance between two Objects.unitsaccepts any insunits value (Centimeters, Feet, Parsecs, and so on) or the abbreviations mm, cm, m, km, ft.distance_modeaccepts bbox_center, bbox, or exact.parts(): returns this object’s sub-elements.parent(): the opposite ofparts(), returns the parent of a sub-element.plies(): returns the associatedPliesiterable.within_distance(distance, unit='mm', distance_mode='exact', search_range=bim_model): returns objects within that distance (sameunits/distance_modeoptions asdistance_to).openings(): returns associated openings.spaces(): returns associated spaces.bounding_elements(): returns associated bounding elements (for space objects).select()/deselect(): adds or removes the Object from the selection.__eq__()and__hash__(): make Objects interoperable with Python sets or dictionaries.
bricscad.bim.Objects class (a collection of BIM Objects):
filter(function): filters using a function parameter.filter(**conditions): filters using keyword-argument conditions.parts()/parents(): returns the parts, or parent objects, of every element in the range.within_distance(distance, unit='undefined', distance_mode='exact', search_range=bim_model): returns objects within distance of any object in the range.openings()/spaces()/bounding_elements(): same as the Object-level methods, applied across the whole range.select()/deselect(): adds or removes every Object in the range from the selection.__len__(): returns the number of Objects in the range.
Module-level functions:
bricscad.bim.list_properties(obj): returns the list of available properties for the given entity or ply.bricscad.bim.current_model(): returns the Objects in the active document’s model space.
bricscad.bim.Plies class: an indexed, sliceable container of Ply objects, supporting __iter__(), __getitem__(index), __getitem__(slice), and __len__().
bricscad.bim.Ply class: an individual ply object, with get_property(prop_name) returning its property value by name.
Frequently Asked Questions
Do I need to install Python separately to use BIMPYTHON? No, Python and its standard libraries are already embedded in BricsCAD BIM; only custom packages need separate installation.
Where do I write and edit my Python scripts? In an external text or code editor, since BricsCAD doesn’t ship its own Python Shell.
How do I find every property available on a BIM object? Use bricscad.bim.list_properties(obj), which returns the full list of available properties for that entity or ply.
Can I chain multiple filtering and query operations together? Yes, bricscad.bim.Objects mappers are chainable, letting you combine filters, parts(), and within_distance() in sequence.