Using Python scripts

About PIP command

With the PIP command, you can install and manage Python packages, for example, custom packages that are not part of the Python standard library.

About BIMPYTHON command

With the BIMPYTHON command, you can query and manage data from a model within BricsCAD BIM, using a suitable Python (*.py) script.

The Python scripts can be as simple as obtaining quantities and associated properties of objects and BIM entities, or as elaborate as implementing a series of calculations based on the model parameters available.

Several sample Python scripts are available in the BricsCAD BIM installer, in the C:\Users\%username%\AppData\Roaming\Bricsys\BricsCAD\V27x64\en_US\Support\Bim\Scripts folder:

  • calculate_weight.py allows calculating the weight of entities based on their volume and density.
  • schedules.py allows generating custom schedules and exporting them to CSV files.
  • set_name_and_number.py populates the name and number properties based on the story, BIM type, and their location.
  • tag_to_number.py allows copying the ID number, if part of the Name (after an IFC import), to the Number property.
  • thermal_transmittance.py calculates the thermal transmittance based on the thermal conductivity of the plies.

You can also use scripts from within the Files tab of the BIM Project Browser panel (see the BIM Project Browser panel article).

Note:
  • BricsCAD does not ship with a Python Shell, so the scripts need to be prepared in a text or code editor application.
  • The Python Programming Language, together with its standard libraries, is embedded within BricsCAD BIM so there is no need for you to install them separately unless you have custom packages and libraries which you wish to use in your scripts.

Executing a Python script

  1. Open a new or a BricsCAD file where you would like to run a Python script.
  2. Type BIMPYTHON in the Command line and press Enter.

    The Select Python script dialog box opens.

  3. Select a PY file and click Open to execute it.
    Note: If the script contains modules that are not already installed, BricsCAD displays a message listing their names. Use the PIP command to install them.
  4. Unless you have specified the data to be exported or displayed in an external program outside BricsCAD, BricsCAD reports the output in the Command line.

Setting up a Python script

Use the text or code editor application of your choice to prepare the script. Open a new file, write the necessary code as exemplified bellow, then save the file with the '.py' extension.

  1. Before gaining access to the API, import or 'call' any desired modules you wish to have in the script.
    Note: If the modules are not part of the Python standard library, make sure they have been installed separately beforehand. For this, you can use the PIP command.

    You can "import" a standard module, such as math and "import as" an external one, for example Pyplot, which is a collection of functions in the matplotlib package:

    import math  
    import matplotlib.pyplot as plt
  2. With your desired libraries "called" in, the next step is to gain access to the API in order to query the elements within the BricsCAD model. This is the range of BIM objects and serves as an entry point to the model.

    Import the current_model from the bricscad.bim module:

    from bricscad.bim import current_model
  3. Now, you can query the model with these example statements:
    # 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) 

    The mappers of the bricscad.bim.Objects are chainable. This is an example to get the parts of the roof that are 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) 

    Whereas this is an example of a function statement to filter:

    # Filter roof parts longer than 50 project units 
    def is_long(obj):  
        return obj.prop('Length') > 50 
    roof_parts.filter(is_long) 
  4. You can also export and show 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('C:\Users\%username%\Desktop\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'C:\Users\%username%\Desktop\file.csv', index = False) 

BricsCAD API Classes and syntaxes

get_property(prop_name)
Returns the property value of the Object with the given name.
set_property(prop_name, value)
Sets a value for the property name (prop_name).
distance_to(other_obj, units='mm', distance_mode='exact')
  • Calculates the distance between two Objects.
  • Options for argument units: any units value from insunits ('Centimeters', 'Feet', 'Parsecs' etc.) and the abbreviations 'mm', 'cm', 'm', 'km' and 'ft'.
  • Options for argument distance_mode: 'bbox_center', 'bbox', exact'.
parts()
Return the sub-elements of this object.
parent()
The opposite of parts(), returns the parent object of this sub-element.
plies()
Returns the associated Plies iterable object.
within_distance(distance, unit='mm', distance_mode='exact', search_range=bim_model)
  • Returns the objects that are within the distance of argument distance.
  • Options for argument units: see distance_to.
  • Options for argument distance_mode: see distance_to.
openings()
Returns the associated openings of this object.
spaces()
Returns the associated spaces of this object.
bounding_elements()
Returns the associated bounding elements of this (space) object.
select()
Adds Object to selection.
deselect()
Removes Object from selection.
__eq__() and __hash__()
Makes the Object interoperable, for example, with a Python set or dictionary.
class bricscad.bim.Objects
Defines collection of BricsCAD BIM Objects
filter(function)
Filters this range with a function parameter.
filter( **conditions )
Filters this range with conditions given as keyword arguments.
parts()
Returns the parts of all the elements in this range.
parents()
Returns the parent objects of elements in this range.
within_distance(distance, unit='undefined', distance_mode='exact', search_range=bim_model)
Returns those objects that are within distance to any object in this range.
openings()
Returns the associated openings of these objects.
spaces()
Returns the associated spaces of these objects.
bounding_elements()
Returns the associated bounding elements of these (space) objects.
select()
Adds Objects to selection.
deselect()
Removes Objects from selection.
__len__ ()
Returns the number of Objects in this range.
bricscad.bim.list_properties(obj)
Returns the list of available properties of the passed entity or ply.
bricscad.bim.current_model()
Returns the Objects in the model space of the active document.
class bricscad.bim.Plies
Indexed, sliceable container of Ply objects:
__iter__()
__getitem__(index)
__getitem__(slice)
__len__()
class bricscad.bim.Ply
Individual ply object.
get_property(prop_name)
Returns the property value of the Ply object with the given name.