Bug: body opacity resets

Bug: body opacity resets

rolandas_vegis
Advocate Advocate
729 Views
4 Replies
Message 1 of 5

Bug: body opacity resets

rolandas_vegis
Advocate
Advocate

Hello,

I encountered a weird bug, in the command upon body selection I change its opacity. When selecting the faces of the body, the opacity of that body changes back for some reason. This does not happen if body selection input has lower limit of more than 0.

 

Demo code:

#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <sstream>

using namespace adsk::core;
using namespace adsk::fusion;

Ptr<Application> app;
Ptr<UserInterface> ui;

// InputChange event handler.
class OnInputChangedEventHander : public InputChangedEventHandler
{
public:
	void notify(const Ptr<InputChangedEventArgs>& eventArgs) override
	{
		Ptr<CommandInput> cmdInput = eventArgs->input();
		if (!cmdInput)
			return;

		if (cmdInput->id() == "DemoBodySelectionInputId")
		{
			Ptr<SelectionCommandInput> selectionInput = cmdInput;
			if (selectionInput->selectionCount() > 0)
			{
				Ptr<BRepBody> body = selectionInput->selection(0)->entity();
				body->opacity(0.4);
			}
		}
	}
};

// CommandCreated event handler.
class CommandCreatedEventHandler : public adsk::core::CommandCreatedEventHandler
{
public:
	void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) override
	{
		if (eventArgs)
		{
			// Get the command that was created.
			Ptr<Command> command = eventArgs->command();
			if (command)
			{
				// Connect to the input changed event.
				Ptr<InputChangedEvent> onInputChanged = command->inputChanged();
				if (!onInputChanged)
					return;
				const auto isOk = onInputChanged->add(&onInputChangedHandler);
				if (!isOk)
					return;

				const auto commandInputs = command->commandInputs();

				const auto bodySelection = commandInputs->addSelectionInput("DemoBodySelectionInputId", "Body selection", "");
				bodySelection->addSelectionFilter("Bodies");
				bodySelection->setSelectionLimits(0, 1); //Changing minimum to 1 solves the problem for some reason

				const auto faceSelection = commandInputs->addSelectionInput("DemoFaceSelectionInputId", "Face selection", "");
				faceSelection->addSelectionFilter("Faces");
				faceSelection->setSelectionLimits(1);
			}
		}
	}

private:
	OnInputChangedEventHander onInputChangedHandler;
} _cmdCreatedHandler;


extern "C" XI_EXPORT bool run(const char* context)
{
	app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	// Create the command definition.
	Ptr<CommandDefinitions> commandDefinitions = ui->commandDefinitions();
	if (!commandDefinitions)
		return nullptr;

	// Get the existing command definition or create it if it doesn't already exist.
	Ptr<CommandDefinition> cmdDef = commandDefinitions->itemById("cmdInputsSample");
	if (!cmdDef)
	{
		cmdDef = commandDefinitions->addButtonDefinition("cmdInputsSample",
		                                                 "Command Inputs Sample",
		                                                 "Sample to demonstrate various command inputs.");
	}

	// Connect to the command created event.
	Ptr<CommandCreatedEvent> commandCreatedEvent = cmdDef->commandCreated();
	if (!commandCreatedEvent)
		return false;
	commandCreatedEvent->add(&_cmdCreatedHandler);

		// Execute the command definition.
	cmdDef->execute();

	
	return true;
}

#ifdef XI_WIN

#include <windows.h>

BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
	switch (reason)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

#endif // XI_WIN

 

Screencast:

https://autode.sk/3cKxQ6t

0 Likes
Accepted solutions (1)
730 Views
4 Replies
Replies (4)
Message 2 of 5

goyals
Autodesk
Autodesk

You can try setting the opacity in command execute Handler. Please take a look at this help documentation http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-3922697A-7BF1-4799-9A5B-C8539DF57051. Thanks.



Shyam Goyal
Sr. Software Dev. Manager
Message 3 of 5

rolandas_vegis
Advocate
Advocate

Well the command I'm creating uses graphics which can be inside the body, so opacity is needed as soon as the body is selected, so that the user could see what is happening.

 

And it is a bug because selection limits impact it for some reason, so it can work just not in the way I need it to, as I need selection limits to be 0. So is it not gonna be fixed?

0 Likes
Message 4 of 5

BrianEkins
Mentor
Mentor
Accepted solution

I don't believe this is a bug but is a side effect of how commands work.  You've probably noticed that any actions you do within a command are grouped together as a single undo operation.  Internally, this is referred to as a transaction.  Each transaction can be undone and shows up in the undo list.  While your command is running, Fusion starts an internal transaction and is adding any operations you perform into that transaction.  If the user edits any of the command inputs, that transaction is aborted (which is basically the same as undo) to take you back to the beginning.  In your case, you changed the opacity but then the next edit in the dialog caused that change to be aborted.  Any change that you might have made would behave the same.

 

To get the behavior you're looking for, you need to keep track of the changes you're making and apply them again each time any change is made to the command inputs.  Typically this is done in the executePreview event.

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

rolandas_vegis
Advocate
Advocate

Yes, your approach works. Didn't think of doing it this way. Thank you.

0 Likes