edit model parameter of imported file

edit model parameter of imported file

nClarke0101
Contributor Contributor
569 Views
3 Replies
Message 1 of 4

edit model parameter of imported file

nClarke0101
Contributor
Contributor

Just as the title says, Trying to edit the parameter of imported file.

My code seems to work, But if the Model parameter name is already being used it will crash my program

 

	Ptr<FileDialog> fileDialog = ui->createFileDialog();
	if (!fileDialog)
		return false;

	fileDialog->filter("fusion File (*.f3d);;All files (*.*)");
	if (fileDialog->showOpen() == adsk::core::DialogResults::DialogOK)
	{
		std::string filename = fileDialog->filename();

		Ptr<Component> rootComp = des->rootComponent();
		if (!rootComp)
			return false;
		
		Ptr<FusionArchiveImportOptions> archive_options = im->createFusionArchiveImportOptions(filename);
		if (!archive_options)
			return false;

		im->importToTarget(archive_options, rootComp);

		size_t importOccurenceCount = rootComp->occurrences()->count();
		Ptr<Occurrence> importOccurence = rootComp->occurrences()->item(importOccurenceCount - 1);
		if (!importOccurence)
			return false;

		Ptr<ModelParameters> modelParameters = importOccurence->component()->modelParameters();
		if (!modelParameters)
			return false;

		std::string name = "test_" + std::to_string(importOccurenceCount - 1);
		if((importOccurenceCount-1) > 0)
			modelParameters->itemByName(name)->value(11);
		else
			modelParameters->itemByName("test")->value(11);
	}
	else
		ui->messageBox("Canceled file dialog.");
0 Likes
570 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor

I think the problem is with your logic in determining the name of the parameter. If the ModelParameters.itemByName method fails, it returns null, which will cause the value function to fail. I'm fairly certain that's what is happening.

 

Even though model parameters are owned by a specific component, their names must be unique with respect to the design. When you import an f3d, Fusion will create unique parameter names to guarantee they are unique. I don't know what the design looks like that you're importing so it's difficult to give specific advice about how to possibly determine the correct name.

 

However, I would recommend using the importToTarget2 method, which returns the occurrences that were created by the import. You can then search those components for the parameters you're looking for.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 4

nClarke0101
Contributor
Contributor

I need a little help with importToTarget2

it returns the ObjectCollection but how do i get the Parameters from that?

0 Likes
Message 4 of 4

BrianEkins
Mentor
Mentor

The importToTarget2 method returns an ObjectCollection of Occurrence objects.  Here's a small sample I used to test the process. I have a local file that contains model parameters named "Length", "Width", and "Height". This imports the f3d into the currently active design and then changes the parameters associated with the imported occurrences.

 

def ImportTest():
    try:
        des: adsk.fusion.Design = app.activeProduct
        root = des.rootComponent
        
        impMgr = app.importManager
        f3dImpOpt = impMgr.createFusionArchiveImportOptions('C:\Temp\ImportParamTest.f3d')
        results: adsk.core.ObjectCollection
        results = impMgr.importToTarget2(f3dImpOpt, root)

        occ: adsk.fusion.Occurrence = results[0]
        comp = occ.component

        param: adsk.fusion.ModelParameter
        for param in comp.modelParameters:
            app.log(f'{param.name}, {param.expression}')
            if param.name.startswith('Length'):
                param.value = 10
            elif param.name.startswith('Width'):
                param.value = 6
            elif param.name.startswith('Height'):
                param.value = 1
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes