Demonstrates how to generate view and manufacturing information files for the imported model, that can be easily acessed through web interface for downstream analysis.
Overview
The example demonstrates how to create a console application to convert your STEP model to compressed *.mtkweb format, generate useful manufacturing information about it with chosen data processor into *.json file). The model will be imported by ModelData_ModelReader . Then the choosen by input parameter certain part processor class will analyze a model. The list of possible analysis can be found in table below.
Process tools description
Process | Tools | Supported representation | Description |
CNC Machining | Machining_FeatureRecognizer | BRep | Recognize features such as machining faces, holes, etc. |
DFMMachining_Analyzer | Analyse possible CNC Machining milling – drilling / milling / turning design issues.
|
Sheet Metal | SheetMetal_FeatureRecognizer | Recognize features such as bends, cutouts, holes, etc. |
SheetMetal_Unfolder | Unfold sheet metal models. |
DFMSheetMetal_Analyzer | Analyse possible Sheet Metal design issues.
|
Finally, the converted *.mtkweb model will be saved to ${export_dir}/model.mtkweb
and all computed process data to ${export_dir}/process_data.json
Running the example
Application needs 6 following input arguments to run:
Usage: MTKConverter -i <import_file> -p <process> -e <export_folder> where:
<import_file> -- filename of a model, that will be imported
<process> -- what analysis should be performed on model
<export_folder> -- export folder name where an output will be saved
Possible process parameter values
Parameter value | Process | Description |
machining_milling | CNC Machining | Milling feature recognition and dfm analysis |
machining_turning | Lathe+Milling feature recognition and dfm analysis |
sheet_metal | Sheet Metal | Feature recognition, unfolding and dfm analysis |
Example:
MTKConverter -i C:\\models\\test.step -p machining_milling -e C:\\models\\test
Implementation
The ModelData_ModelReader is used to import the models.
@staticmethod
def __Import(theFilePath: str, theModel: mtk.Model):
print("Importing ", theFilePath, "...", sep="", end="")
aReader = mtk.ModelReader()
if not aReader.Read(mtk.UTF16String(theFilePath), theModel):
print("\nERROR: Failed to import ", theFilePath, ". Exiting", sep="")
return MTKConverter_ReturnCode.MTKConverter_RC_ImportError
return MTKConverter_ReturnCode.MTKConverter_RC_OK
Then Process
method was created for a model analysis with chosen by process_param_table value processor. The ModelData_Model.AssignUuids() method is used to assign persistent id's to unique parts of the model. These id's will be used to connect parts with process data saved in json file. The processor is a MTKConverter_PartProcessor
class, that can analyse all parts of the imported model. The ProcessType
method can parse a process_param_table parameter value to choose MTKConverter_PartProcessor
class, which will run relative analyzer tools. The ModelData_ModelElementUniqueVisitor
is used to traverse and retrieve only unique parts of the imported model.
@staticmethod
def __Process (theProcess: str,
theModel: mtk.Model,
theReport: MTKConverter_Report,
theProcessModel: mtk.Model):
print("Processing ", theProcess, "... ", sep="", end="")
theModel.AssignUuids()
aProcessType = MTKConverter_Application.__ProcessType(theProcess)
if aProcessType == MTKConverter_ProcessType.MTKConverter_PT_MachiningMilling:
aProcessor = MTKConverter_MachiningProcessor(mtk.Machining_OT_Milling)
MTKConverter_Application.__ApplyProcessorToModel(aProcessor, theModel, theReport)
elif aProcessType == MTKConverter_ProcessType.MTKConverter_PT_MachiningTurning:
aProcessor = MTKConverter_MachiningProcessor(mtk.Machining_OT_LatheMilling)
MTKConverter_Application.__ApplyProcessorToModel(aProcessor, theModel, theReport)
elif aProcessType == MTKConverter_ProcessType.MTKConverter_PT_Molding:
aProcessor = MTKConverter_MoldingProcessor()
MTKConverter_Application.__ApplyProcessorToModel(aProcessor, theModel, theReport)
elif aProcessType == MTKConverter_ProcessType.MTKConverter_PT_SheetMetal:
anUnfoldedName = str(theModel.Name()) + "_unfolded"
theProcessModel.SetName(mtk.UTF16String(anUnfoldedName))
aProcessor = MTKConverter_SheetMetalProcessor(theProcessModel)
MTKConverter_Application.__ApplyProcessorToModel(aProcessor, theModel, theReport)
else:
return MTKConverter_ReturnCode.MTKConverter_RC_InvalidArgument
return MTKConverter_ReturnCode.MTKConverter_RC_OK
Before we dive into how the model is processed with MTKConverter_PartProcessor
class after the ApplyProcessorToModel was ran, the inheritance hierarchy should be observed.
-
First, the MTKConverter_PartProcessor
class, which inherits from ModelData_ModelElementVoidVisitor , was created with the overridden void operator()(const ModelData::Part& thePart)
method to traverse the model and collect each ModelData_Part . Then, ModelData_Solid and ModelData_Shell are extracted using ModelData_ModelElementVoidVisitor . The ProcessSolid
, ProcessShell
is developed to analyze the certain shapes of ModelData_Part . Additionally, some analyses require post-processing, which is handled by the PostPartProcess
method. These four methods will be described in detail later.
def __call__(self, thePart: mtk.Part):
aBodyList = thePart.Bodies()
for aBody in aBodyList:
aShapeIt = mtk.ShapeIterator(aBody)
for aShape in aShapeIt:
if aShape.Type() == mtk.ShapeType_Solid:
self.ProcessSolid(thePart, mtk.Solid.Cast(aShape))
elif aShape.Type() == mtk.ShapeType_Shell:
self.ProcessShell(thePart, mtk.Shell.Cast(aShape))
self.PostPartProcess (thePart)
MTKConverter_VoidPartProcessor
was inherited from MTKConverter_PartProcessor
at first with empty defined methods for processing, because not all processor can analyse Shell, Mesh and requires postprocessing. Then for each process different class, inherited from MTKConverter_VoidPartProcessor
, was developed. The table below demonstrates what classes were created for each process and which methods were overriden.
Processor classes
Process | Class | ProcessSolid | ProcessShell | ProcessMesh | PostPartProcess |
CNC Machining | MTKConverter_MachiningProcessor | + | - | - | - |
Sheet Metal | MTKConverter_SheetMetalProcessor | + | - | + |
-
Let's take the MTKConverter_SheetMetalProcessor
as example, because it supports almost all methods of MTKConverter_PartProcessor
. It uses SheetMetal_Analyzer for shape processing. In constructor both supported tools (SheetMetal_FeatureRecognizer, SheetMetal_Unfolder) were added to SheetMetal_Analyzer. Analyzer classes can be used to run all tools together, however it should be mentioned, that such tools allow separated calls. The example of such separate call for SheetMetal_Unfolder can be found here.
def __init__(self, theUnfoldedModel: mtk.Model):
super().__init__()
self.myAnalyzer = mtk.SheetMetal_Analyzer()
self.myUnfoldedModel = theUnfoldedModel
self.myCurrentUnfoldedBody = mtk.SheetBody()
self.myAnalyzer.AddTool(mtk.SheetMetal_FeatureRecognizer())
self.myAnalyzer.AddTool(mtk.SheetMetal_Unfolder())
The Perform
method of SheetMetal_Analyzer will be called to run analysis with added tools for Solid and Shell respectively.
def ProcessSolid (self, thePart: mtk.Part, theSolid: mtk.Solid):
anSMData = self.myAnalyzer.Perform(theSolid)
self.__UpdateProcessData(anSMData, thePart)
def ProcessShell (self, thePart: mtk.Part, theShell: mtk.Shell):
anSMData = self.myAnalyzer.Perform(theShell)
self.__UpdateProcessData(anSMData, thePart)
After the analysis was done the PostPartProcess
method will be run.
def PostPartProcess(self, thePart: mtk.Part):
if not self.myCurrentUnfoldedBody:
return
anUnfoldedPart = mtk.Part(thePart.Name())
anUnfoldedPart.SetUuid(thePart.GetUuid())
anUnfoldedPart.AddBody(self.myCurrentUnfoldedBody)
self.myUnfoldedModel.AddRoot(anUnfoldedPart)
self.myCurrentUnfoldedBody = mtk.SheetBody()
Finally, after the processing, the model will be converted to *.mtkweb format.
@staticmethod
def __Export(theFolderPath: mtk.UTF16String,
theModel: mtk.Model,
theReport: MTKConverter_Report,
theProcessModel: mtk.Model):
print("Exporting ", theFolderPath, "...", sep="", end="")
os.mkdir(theFolderPath)
if not theModel.Save(mtk.UTF16String(aModelPath), mtk.Model.FileFormatType_MTKWEB):
print("\nERROR: Failed to export ", aModelPath, ". Exiting", sep="")
return MTKConverter_ReturnCode.MTKConverter_RC_ExportError
return MTKConverter_ReturnCode.MTKConverter_RC_OK
An unfolded representation, if the SheetMetal process was performed also will be converted to *.mtkweb format and saved.
if not theProcessModel.IsEmpty():
aProcessModelPath = theFolderPath + "/" + str(theProcessModel.Name()) + ".mtkweb" + "/scenegraph.mtkweb"
if not theProcessModel.Save(mtk.UTF16String(aProcessModelPath), mtk.Model.FileFormatType_MTKWEB):
print("\nERROR: Failed to export ", aProcessModelPath, ". Exiting", sep="")
return MTKConverter_ReturnCode.MTKConverter_RC_ExportError
A result of processing will be saved to a json file with the custom MTKConverter_Report
class. However, other premade solutions can be used too.
aJsonPath = theFolderPath + "\\process_data.json"
if not theReport.WriteToJSON (aJsonPath):
print("\nERROR: Failed to create JSON file ", aJsonPath, ". Exiting", sep="")
return MTKConverter_ReturnCode.MTKConverter_RC_ExportError
return MTKConverter_ReturnCode.MTKConverter_RC_OK
Most of MTKConverter_Report
methods were designed to generate a json file itself, but there are several distinct methods:
-
WriteFeatures
run sorts and generates a text representation of features.
-
SortFeatures
sorts and groups features to make an output compact. The MTKBase_FeatureComparator
was used for comparison of features by types and parameters. An order of features is preserved with a map.
-
GetShapesId
gets the id of a Shape for a BRepRepresentation. A relative Shape of converted model can be found with the Id.
-
Methods like AddShapeFeature
/ AddDrillingIssue
/ etc. make a text representation of features with their relative parameters.
All features have the same to text export pattern, therefore only the example for Machining_Countersink was present.
elif mtk.Machining_Countersink.CompareType(aFeature):
aCountersink = mtk.Machining_Countersink.Cast(aFeature)
anAxis = aCountersink.Axis().Axis()
aDirection = Direction(anAxis.X(), anAxis.Y(), anAxis.Z())
aFeatureData = MTKConverter_Report.__WriteFeatureDataToString3(
"Radius", "mm", aCountersink.Radius(),
"Depth", "mm", aCountersink.Depth(),
"Axis", "", aDirection,
theShapeIdVector)
theManager.AddGroupData("Countersink(s)", "(55, 125, 34)", aFeatureData, theCount)
Example output
Below are outputs for different processes.
Machining Milling
The ./examples/models/Fresamento_CAM1_v3.stp
model can be used to run machining_milling
process.
Original Model
|
Processed Model
|
Output
{
"version": "1",
"parts": [
{
"partId": "e31092b7-fc4e-4c02-8021-c0bbbcd21bfa",
"process": "CNC Machining Milling",
"featureRecognition": {
"name": "Feature Recognition",
"totalFeatureCount": "51",
"featureGroups": [
{
"name": "Concave Fillet Edge Milling Face(s)",
"color": "(129, 127, 38)",
"totalGroupFeatureCount": "14",
"subGroupCount": "1",
"subGroups": [
{
"parametersCount": "1",
"parameters": [
{
"name": "Radius",
"units": "mm",
"value": "5.00"
}
],
"featureCount": "14",
"features": [
{
"shapeIDCount": "1",
"shapeIDs": [
{
"id": "555"
}
]
},
...
{
"shapeIDCount": "1",
"shapeIDs": [
{
"id": "669"
}
]
}
]
}
]
},
...
"dfm": {
"name": "Design for Manufacturing",
"totalFeatureCount": "23",
"featureGroups": [
{
"name": "Deep Hole(s)",
"color": "(0, 35, 245)",
"totalGroupFeatureCount": "4",
"subGroupCount": "1",
"subGroups": [
{
"parametersCount": "2",
"parameters": [
{
"name": "Expected Maximum Depth",
"units": "mm",
"value": "29.84"
},
{
"name": "Actual Depth",
"units": "mm",
"value": "31.32"
}
],
"featureCount": "4",
"features": [
{
"shapeIDCount": "2",
"shapeIDs": [
{
"id": "435"
},
{
"id": "423"
}
]
},
{
"shapeIDCount": "2",
"shapeIDs": [
{
"id": "450"
},
{
"id": "438"
}
]
},
{
"shapeIDCount": "2",
"shapeIDs": [
{
"id": "465"
},
{
"id": "453"
}
]
},
{
"shapeIDCount": "2",
"shapeIDs": [
{
"id": "480"
},
{
"id": "468"
}
]
}
]
}
]
},
...
Machining Turning
The ./examples/models/senthi.step
model can be used to run machining_turning
process. The processing result has the same structure as for a Machining milling, thus it was omitted.
Original Model
|
Processed Model
|
Sheet Metal
The ./examples/models/Part2.stp
is suitable to run SheetMetal process. The first part of a json file is the same as in Machining milling and turning, but it also contains the infromation about unfolding analysis.
Original Model
|
Unfolded Model
|
Output
...
"featureRecognitionUnfolded": {
"name": "Feature Recognition",
"parametersCount": "3",
"parameters": [
{
"name": "Length",
"units": "mm",
"value": "220.71"
},
{
"name": "Width",
"units": "mm",
"value": "167.84"
},
{
"name": "Thickness",
"units": "mm",
"value": "1.00"
}
]
},
"dfmUnfolded": {
"name": "Design for Manufacturing",
"totalFeatureCount": "1",
"featureGroups": [
{
"name": "Non Standard Sheet Size(s)",
"color": "(0, 0, 0)",
"totalGroupFeatureCount": "1",
"subGroupCount": "1",
"subGroups": [
{
"parametersCount": "2",
"parameters": [
{
"name": "Nearest Standard Size (LxW)",
"units": "mm",
"value": "300.00 x 200.00"
},
{
"name": "Actual Size (LxW)",
"units": "mm",
"value": "220.71 x 167.84"
}
],
"featureCount": "1",
"features": [
{
"shapeIDCount": "0",
"shapeIDs": []
}
]
}
]
}
]
}
}
]
}
Files