Standard PythonPart
In a standard PythonPart the entire workflow consists of two steps:
- input of the parameters in the property palette
- placing the element in the model
This means that only the parameters provided in the property palette influence the final element. The workflow is already implemented in the PythonParts framework, meaning that you don't need to implement it entirely, just a function for the element creation. This also means, that additional input steps, such as object selection or coordinate input, cannot be introduced. This limits the possibilities, but requires only two functions to be implemented (see required functions below). Tasks like handle creation, showing the palette or creating a preview are managed entirely by the framework.
Beside the creation of the element, you can handle other events, by implementing optional functions, e.g. when the user modifies a certain property in the property palette.
Required functions
-
This function is called at the very beginning to check, whether the PythonPart can be started in the current ALLPLAN version. You can prevent the user for starting the PythonPart in an older ALLPLAN version. You must implement it, even if your script supports all versions.
-
Here you implement the creation of the elements. The input, framework gives you as arguments, is a.o. the BuildingElement, containing all the parameter values from the property palette. You can refer to them as shown here. The output should be the elements, you want to create, put into a data class CreateElementResult
check_allplan_version
check_allplan_version(build_ele: BuildingElement, version: float) -> bool
Called when the PythonPart is started to check, if the current Allplan version is supported.
Parameters:
-
build_ele
(BuildingElement
) –building element with the parameter properties
-
version
(float
) –current Allplan version
Returns:
-
bool
–True if current Allplan version is supported and PythonPart script can be run, False otherwise
Source code in src\PythonPartsScriptTemplates\StandardPythonPart.py
Example
To allow the user to open your script with ALLPLAN 2025 only, you can implement it like this:
create_element
create_element(
build_ele: BuildingElement, doc: DocumentAdapter
) -> CreateElementResult
Function for the element creation
Parameters:
-
build_ele
(BuildingElement
) –the building element.
-
doc
(DocumentAdapter
) –input document
Returns:
-
CreateElementResult
–created element result
Source code in src\PythonPartsScriptTemplates\StandardPythonPart.py
Example
To create a 3D element, being a simple cube, you can implement it like this:
import NemAll_Python_Geometry as AllplanGeometry
from CreateElementResult import CreateElementResult
from TypeCollections.ModelEleList import ModelEleList
def create_element(_build_ele, _doc):
cube = AllplanGeometry.Polyhedron3D.CreateCuboid(length = 1000.0,
width = 1000.0,
height = 1000.0)
elements = ModelEleList()
elements.append_geometry_3d(cube)
return CreateElementResult(elements)
Info
In some old examples you might see that the create_element
function returns a two-element tuple,
consisting of model_elements_list
and handle_list
. Although it works, we recommend returning
the data class CreateElementResult.
It is a container holding the created objects, but offers extra options like:
- multi placement - enabling the user to place multiple instances of the PythonPart in one step,
- global placement point - place the PythonPart in specified point instead of letting the user to place it.
Optional functions
Implementing additional functions allows you to handle other events. For example, if you want to introduce some steps before the palette is being shown (like reading data from an external CSV file), or when a button is pressed in the palette.
Here is an overview of the functions that framework may call from your script, together with the description when these functions are called. Implementing them is either entirely optional, or required by an optional feature.
-
Implement it to introduce some actions before the property palette is shown. For example to read some data from an external file, in order to show it in the property palette.
-
Implement it, if you want to react to the event of the user modifying the values in the property palette. This allows you to make it more responsive and dynamic. Learn more about making the palette dynamic in this chapter
-
Implement it, if you want to introduce an action triggered by pressing a button on the property palette.
-
The implementation of this function is necessary only in case, when the PythonPart should have the docking points feature
-
Implementing this function makes sense only, when your PythonPart uses handles. It allows you to react to the event of a handle being modified by the user.
-
To control the behavior of your PythonPart depending on the currently displayed page of the property palette, implement the function into your script. You may want to show different objects in the preview or only selected handles, depending on what palette page is currently active.
-
By default, the preview in the library is generated by calling the create_element function with default parameter values and displaying the resulting elements. You can override this, by implementing this function and create different elements for the library preview.
-
When you released a new version of your PythonPart and you have changed e.g. the parameter names, implementing this function might be necessary to migrate the old parameters to the new structure and ensure the seamless work when with already placed PythonParts in the modification mode.
create_docking_points
create_docking_points(
build_ele: BuildingElement, doc: DocumentAdapter
) -> Tuple[
List[Tuple[str, Point3D]],
List[Tuple[str, Point3D]],
List[Tuple[str, Point3D]],
]
Creation of the docking points
Parameters:
-
build_ele
(BuildingElement
) –building element with the parameter properties
-
doc
(DocumentAdapter
) –document of the Allplan drawing files
Returns:
-
List[Tuple[str, Point3D]]
–list of tuples (key, docking_point) for the 2D view
-
List[Tuple[str, Point3D]]
–list of tuples (key, docking_point) for the 3D view
-
List[Tuple[str, Point3D]]
–list of tuples (key, docking_point) for the 2D and 3D view
Source code in src\PythonPartsScriptTemplates\StandardPythonPart.py
initialize_control_properties
initialize_control_properties(
build_ele: BuildingElement,
ctrl_prop_util: ControlPropertiesUtil,
doc: DocumentAdapter,
) -> None
Called after the properties and their values are read, but before the property palette is displayed.
Parameters:
-
build_ele
(BuildingElement
) –building element
-
ctrl_prop_util
(ControlPropertiesUtil
) –control properties utility
-
doc
(DocumentAdapter
) –document
Source code in src\PythonPartsScriptTemplates\StandardPythonPart.py
modify_control_properties
modify_control_properties(
build_ele: BuildingElement,
ctrl_prop_util: ControlPropertiesUtil,
value_name: str,
event_id: int,
doc: DocumentAdapter,
) -> bool
Called after each change within the property palette
Parameters:
-
build_ele
(BuildingElement
) –building element
-
ctrl_prop_util
(ControlPropertiesUtil
) –control properties utility
-
value_name
(str
) –name(s) of the modified value (multiple names are separated by ,)
-
event_id
(int
) –event ID
-
doc
(DocumentAdapter
) –document
Returns:
-
bool
–True if an update of the property palette is necessary, False otherwise
Source code in src\PythonPartsScriptTemplates\StandardPythonPart.py
on_control_event
on_control_event(
build_ele: BuildingElement, event_id: int, doc: DocumentAdapter
) -> bool
Called, when an event is triggered with a control (e.g. a button) in a property palette
Parameters:
-
build_ele
(BuildingElement
) –building element with the parameter properties
-
event_id
(int
) –event id of the triggered control
-
doc
(DocumentAdapter
) –document of the Allplan drawing files
Returns:
-
bool
–True if palette refresh is necessary, False otherwise
Source code in src\PythonPartsScriptTemplates\StandardPythonPart.py
move_handle
move_handle(
build_ele: BuildingElement,
handle_prop: HandleProperties,
input_pnt: Point3D,
doc: DocumentAdapter,
) -> CreateElementResult
Called after modification of the element geometry using handles
Parameters:
-
build_ele
(BuildingElement
) –building element with the parameter properties
-
handle_prop
(HandleProperties
) –handle properties
-
input_pnt
(Point3D
) –input point
-
doc
(DocumentAdapter
) –input document
Returns:
-
CreateElementResult
–Object with the result data of the element creation
Source code in src\PythonPartsScriptTemplates\StandardPythonPart.py
set_active_palette_page_index
Parameters:
-
active_page_index
(int
) –index of the active page, starting from 0
create_preview
create_preview(
build_ele: BuildingElement, doc: DocumentAdapter
) -> CreateElementResult
Function for the creation of the library preview elements.
Parameters:
-
build_ele
(BuildingElement
) –the building element.
-
doc
(DocumentAdapter
) –input document
Returns:
-
CreateElementResult
–Preview elements. Only elements included in the property "elements" will be shown in the library preview
Source code in src\PythonPartsScriptTemplates\StandardPythonPart.py
migrate_parameter
Migrate the parameter
This function is called only during the modification of a PythonPart
Parameters:
-
parameter_list
(list
) –parameter list
-
version
(float
) –current PythonPart version