Export model macro

Export model macro

iamcdn79
Mentor Mentor
4,537 Views
9 Replies
Message 1 of 10

Export model macro

iamcdn79
Mentor
Mentor

I am working on a macro that exports a model from a list to my project directory.

 

1 problem I have is it does not export the model I selected from the list.

 

ENTITY LIST $Selected_Model = INPUT ENTITY MULTIPLE MODEL "Select Models For Export"

FOREACH Mod IN $Selected_Model {
STRING projName = project_pathname (0) 
STRING $savePath = $projName + ".dgk"
IF dir_exists ($savePath) { 
MESSAGE WARN "Project with this path already exists" + crlf + $savePath 
RETURN 
} 
STRING $com = "EXPORT MODEL $Selected_Model FILESAVE '" + $savePath + "'" 
DOCOMMAND $com

 

If I replace line 10 with 

 

STRING $com = "EXPORT MODEL ALL FILESAVE '" + $savePath + "'"
 
 it works but it does every model I have in the project.  What do I need to get the selected model from the list?

 

Problem #2 is when it does export all the models it uses my Powermill project name as the model name. I would like it to use the actual model name listed inside Powermill.


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Accepted solutions (1)
4,538 Views
9 Replies
Replies (9)
Message 2 of 10

Sean571
Advocate
Advocate
    // Reset any local variables
    reset localvars

    // Variables
    string $prompt = ""
    real $exportTolerence = 0
    bool err = 0    
    string saveFolder = INPUT "Enter Directory path to Export Models:"
    string saveFilePath = ""

    // Save original export tolerence
    real originalExportTolerence = $powermill.Export.TriangleTolerance

    // SET TO WORLD COORIDATES
    DEACTIVATE Workplane

    FOREACH mod IN folder('model') {
        $prompt = "Export Tolerence for " + $mod.name
        
        // Error checking on export tolerence
        DO {
            $exportTolerence = INPUT $prompt
            $err = ERROR $exportTolerence
            IF $err {
                MESSAGE ERROR "Input not a number! Try again."
            }
        } WHILE $err == true
        
        // Set tolerence
        EDIT PAR Powermill.Export.TriangleTolerance $exportTolerence

        // build export file path
        $saveFilePath = $saveFolder + $mod.name + ".dmt"
        DELETE FILE $saveFilePath
        EXPORT MODEL $mod.name $saveFilePath
 
    }


Here is a different macro I made for exporting models. Maybe this can help you.

Sean Wroblewski
Applications Engineer

0 Likes
Message 3 of 10

iamcdn79
Mentor
Mentor

Thanks Sean,

 

After tweaking your macro it's a little bit better but I still can't get the selected model to export, it writes out every model in the project and now it combines the project name and model name as the export filename.

 

// Reset any local variables
    reset localvars

    // Variables
    string $prompt = ""
    real $exportTolerence = 0
    bool err = 0    
    string saveFolder = project_pathname (0) 
    string saveFilePath = ""
ENTITY LIST $Selected_Model = INPUT ENTITY MULTIPLE MODEL "Select Models For Export"
    // Save original export tolerence
    real originalExportTolerence = $powermill.Export.TriangleTolerance

    // SET TO WORLD COORIDATES
    DEACTIVATE Workplane

    FOREACH mod IN folder('model') {
       // $prompt = "Export Tolerence for " + $mod.name
        
        // Error checking on export tolerence
        // DO {
        //     $exportTolerence = INPUT $prompt
        //     $err = ERROR $exportTolerence
        //     IF $err {
        //         MESSAGE ERROR "Input not a number! Try again."
        //     }
        // } WHILE $err == true
        
        // Set tolerence
        EDIT PAR Powermill.Export.TriangleTolerance $exportTolerence

        // build export file path
        $saveFilePath = $saveFolder + $mod.name + ".dgk"
        DELETE FILE $saveFilePath
        EXPORT MODEL $mod.name $saveFilePath
 
    }

Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 4 of 10

Sean571
Advocate
Advocate

You should be doing that foreach loop with the list of selected models, not with all the models and that should work for you.

// Reset any local variables
reset localvars

// Variables
string saveFolder = project_pathname (0) 
string saveFilePath = ""
ENTITY LIST $Selected_Model = INPUT ENTITY MULTIPLE MODEL "Select Models For Export"

// SET TO WORLD COORIDATES
DEACTIVATE Workplane

FOREACH mod IN $Selected_Model {
    // build export file path
    $saveFilePath = $saveFolder + $mod.name + ".dgk"
    DELETE FILE $saveFilePath
    EXPORT MODEL $mod.name $saveFilePath
}
Sean Wroblewski
Applications Engineer

0 Likes
Message 5 of 10

artur.boszczyk
Advocate
Advocate

why do waste your time building something which already exists in PowerMill??? Surely you know how to export models using PowerMill interface without using API

 

Use API to extend the functionality of the PowerMill and then share it with all people over here.

0 Likes
Message 6 of 10

kmarkopouliotis
Advocate
Advocate

Hi,

I'm trying to develop a macro that is going to Export as

"Autodesk Geometry(*.dgk)" AND "Autodesk Geometry + Features (*.dgk)"

 a selected component (or model).

The MAJOR PROBLEM is that i cannot get for each case the Save as type option.

I've recorded 2 seperate macros in order to export as (1st) "Autodesk Geometry(*.dgk)" and (2nd) "Autodesk Geometry + Features (*.dgk)".

These to macros have exactly the same code.

Does anyone have any idea on how to get Save as type "value" for exporting as "Autodesk Geometry + Features (*.dgk)".

 

Thank you all.

0 Likes
Message 7 of 10

kmarkopouliotis
Advocate
Advocate

Hi,

My purpose is to Export a selected Solid or Component in 4 formats [Save as type] :

  1. Parasolid(*.x_t)
  2. Adobe Acrobat Document (*.pdf)
  3. Autodesk Geometry (*.dgk)
  4. Autodesk Geometry + Features (*.dgk) [provides an *.xml with all features]I want to Export Solid's/Component's Feature's in order to make use of the in PowerMill.

Does anyone have another option in order to export features ???

0 Likes
Message 8 of 10

iamcdn79
Mentor
Mentor

It exports the selected models from the list but it still adds the project name to the exported model name.

 

iamcdn79_0-1666267255388.png

 

Also, it is missing a warning to say that the file\files exists, do you want to overwrite it?


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

Message 9 of 10

Sean571
Advocate
Advocate

@artur.boszczyk  Powermill macros are intended to automate processes and reduce the amount of clicks that a user has to make while working, as well as extend it's functionality a tad bit. Writing a small macro to automatically export multiple models to the same location as the project while also renaming them is a perfectly reasonable goal for a macro.

Now, if I was actually using the API, and not the macro language then I would probably not be creating an application solely to export models in this fashion.

Sean Wroblewski
Applications Engineer

0 Likes
Message 10 of 10

Y.Mahran
Contributor
Contributor
Accepted solution

Hi @iamcdn79 , I have a macro that does a similar functionality to what you want. I hope it will be helpful to you.

 

 

1) It creates a folder in your PowerMill project (If not created already).

 

2) It asks the user to choose whether to export some models from the list or export all of them. And, of course, it names the models according to their names in the PM project.

 

// Checking if the project contain models or not
RESET LOCALVARS
ENTITY List currentModels = folder('model')
IF (is_empty(currentModels)) {
  MESSAGE ERROR "The models folder is empty!"
  MACRO ABORT
}


// Creating a folder to export models into
// and Changing the save Directory to it
STRING projectPath = project_pathname(0)
STRING exportedModelsFolder = $projectPath + "/Exported_Models"
IF (dir_exists($exportedModelsFolder)==0) {
  MKDIR $exportedModelsFolder
}
CD $exportedModelsFolder


// Choose the export mode (Selected models/All models)
ENTITY LIST modelsToExport = {}
STRING ARRAY modelsExportMode[] = {"Select models to export", "Export all models"}
INT exportMode = INPUT CHOICE $modelsExportMode "Select the export mode:"
SWITCH $exportMode {
  CASE 0
	// Select certain models to export
	$modelsToExport = INPUT ENTITY MULTIPLE model "Select models to export:"
	BREAK
  CASE 1
	// Select all models 
	$modelsToExport = folder('model')	
	BREAK
}


// Exporting models
FOREACH model IN modelsToExport {
	STRING modelToExport = $model.name		
	STRING modelSaveName = $modelToExport + ".dgk" // you may change ".dgk" to ".dmt" if needed

	// Checking if file already exists & requesting permession to overwrite
	IF (file_exists($modelSaveName)) {
		STRING queryMSG = "File (" + $modelSaveName + ") already exist, Overwite it?"
		BOOL queryAnswer = 0
		$queryAnswer = Query $queryMSG
		IF (queryAnswer==1) {
			DELETE FILE $modelSaveName
		} ELSE {
			CONTINUE
		}
	}
	EXPORT MODEL $modelToExport $modelSaveName
}