Creating User Defined Primitives and User Defined Models in Python Scripts
You can create User Defined Primitives and User Defined Models in Python scripts (based on the IronPython implementation).
Advantages Compared to C++
- No need to create and build project; all you need to do is create a Python script
- Python script is platform independent
- Scripts can inherit functionality from existing scripts
- Garbage collector - no need to free memory
- Easy debugging
Changes compared to C
Though methods, constants and structures are kept as close to the C implementation as possible, some changes had to be made to make code Python-compatible.
Structures
- Structures have the same names as in C implementation.
- Structures fields names are capitalized.
- Arrays in structures become lists in Python (Technically a.NET IList container)
- Structure instances are created using the supplied constructors and members are accessed using the provided access methods.
For a complete list of structures and examples please see UDP/UDM Structures.
Return Values for UDM and UDP Functions
For information on return values for each UDM and UDP function, see the Return Values section.
Constants
Enumeration/Enum constants have almost the same names as in C but the enum must be qualified by the type. Additionally, redundant "UDP", "UDM" or type prefixes have been removed. This allows for better human-readability.
# Example of specifying the LengthUnit enum by qualifying it
# with the type of the enum: UnitType
unitType = UnitType.LengthUnit
For a complete list of enum constants please see UDP/UDM Constants.
Methods
Methods are described in IUDPExtension methods, IUDMExtension methods, and UDMFunctionLibrary listed further in this document. A separate chapter includes a UDP IronPython example of fillet and chamfer.
The main differences in functions parameters (from C implementation):
- functions names in UDPFunctionLibrary and UDMFunctionLibrary are capitalized
- arrays become a python list of objects
void * callback parameteris dropped from the parameter list- output parameters (pointer types that are filled during the function call) usually become return values
- 'list size' parameter usually will be omitted as redundant
Output Parameters
The rule for the output parameters is as follows:
- If the function has one output parameter variable and no return value, the variable will become function's return value.The same will happen if the return value is a 'success/failure' boolean ('None' will be returned on failure and parameter variable - on success).
- If the function has one output parameter and a return value, the function will return a Python tuple where function return value will be the first one in the tuple.
- If there is more than one out variable, the function will return a Python tuple with all output parameters in the specified order. If function has a return value, it must always be the first in the tuple.
# one output parameter; return value is ignored
udmDefinition = udmFunctionLibrary.GetDefinition()
# one output parameter;
return value must be preserved. return
# and output values are packed into the return tupe, in order
(lRet, partIdsList) = udpFunctionLibrary.DetachFaces(nPartIds, faceIdsList)
# Two output parameter; return value must be preserved
# the return tuple is (returnVal, output1, output2)
(bRet, udpPositionLow, udpPositionHigh) = udmFunctionLibrary.GetBoundingBox(partId,exact);
Comparison with C function:
C | Python |
bool getDefinition(UDMDefinition* udmDefinition, void* callbackData );
where udmDefinition is an output parameter
| udmDefinition = udmFunctionLibrary.GetDefinition()
(Note: callbackData is omitted in py interface) |
long detachlFaces( int nFacesAndPartIds, long* faceIds, long* partIds, void* callbackData);
where partIds is an output parameter | (bRet, partIds) = udmFunctionLibrary.DetachlFaces(nFacesAndPartIds, faceIds)
(Note: callbackData is omitted in py interface) |
'List Size' Parameters
The rule for the 'list size' is as follows:
- If function has input 'List' parameter and input 'list size' parameter, 'list size' parameter will be omitted.
- If function has output 'List' parameter and output 'list size' parameter, 'list size' parameter will be omitted.
- If function has output 'List' parameter and input 'list size' parameter, 'list size' parameter won't be omitted as it's needed for memory allocation in the corresponding C++ function from the UDP/UDM function library.
Example:
# input list, input list size
lret = udpFunctionLibrary.Unite(objectIds)
# output list, output list size
faceIdList = udmFunctionLibrary.GetAllFaces(PartId)
# output list, input list size
(lret, partIdList) = udpFunctionLibrary.DetachFaces(listSize, faceIdList)
Comparison with C function:
C | Python |
bool getAllFaces( long partId, long* numFaces, long** faceIds, void* callbackData);
where numFaces and faceIds are output parameters and numFaces is the size of faceId. | faceIds = udmFunctionLibrary.GetAllFaces(partId)
(ignore numFaces as redundant: folded into faceIds, return value is omitted: folded into the faceIds is None check callbackData is omitted) |
long unite( long numObjects, long* objectIds, void* callbackData);
where numObjects and objectIds are input parameters and numObjects is the size of objectIds. | lret = udpFunctionLibrary.Unite(objectIds)
(ignore numObjects as redundant: folded into objectIds callbackData is omitted) |
long detachFaces( long nSize, long* faceIds, long* partIds, void* callbackData);
where partIds is and output list and nSize is an input parameters and nSize is the size of partIds. | (lret, partIdList) = udpFunctionLibrary.DetachFaces(nSize, faceIds)
(nSize is not ignored, callbackData is omitted) |
Added Parameters
There is a special case in UDPFunctionLibrary: two functions - DuplicateAlongLine and DuplicateAroundAxis - have new integer listSize parameter added to their signatures.
This parameter defines the size of the output List. This is done for compliance with C++ geometry library as the size of the List must be predefined and this size is different from the existing parameter’s values.
Example:
(ret, cloneIDs) = funcLib.DuplicateAlongLine(partID, transVec, numCubes, cloneIdsSize)
(ret, cloneIDs) = funcLib.DuplicateAroundAxis(partID, axis, angle, nClones, cloneIdsSize)
Here cloneIdsSize is a new integer parameter.
Comparison with C function:
C | Python |
long duplicateAlongLine( long partId, UDPVector transVector, int nClones, long* nClones, void* callbackData); | (lret, cloneIds) = udmFunctionLibrary.DuplicateAlongLine(partId, transVec, nClones, cloneIdsSize)
(callbackData is omitted cloneIdsSize is a new parameter) |
long duplicateAroundAxis( long partId, UDPCoordinateSystemAxis axis, double angle, int nClones, long* nClones, void* callbackData); | (lret, cloneIds) = udmFunctionLibrary.DuplicateAroundAxis(partId, axis, angle, nClones, cloneIdsSize)
(callbackData is omitted cloneIdsSize is a new parameter) |