Lesson 2: Understand the document

Before begin with learning Fusion API, we’ll take a short detour on how to use the document.

L2-Document-Overview.png

There are several parts of the document. The Fusion API User’s Manual is guides and tips for using the Fusion API. The Fusion API Reference Manual is the document about the API objects and enums.

Let’s have a look at the user manual first.

L2-Fusion-User-Manual-Index.png

Although both C++/Python/Typescript have the same reference document. There are some differences between Python, Typescript and C++ API. The Typescript is more similar to the Python when using it, while C++ has more to do with casting and platform issues. It is highly recommended to read them before checking the Reference Manual.

Fusion scripts/add-ins are the made up by using objects and enums in Fusion API.

L2-Fusion-API-Reference-Manual.png

Most of the objects and features are in the Objects section. For example, if we are going to work with a component, we could find and click at the Component Object. It will tell you how a component object works and what could you achieve with it.

There are two parts of members of an object: Methods and Properties.

Methods could be used to operate with the object. For example, we could use saveCopyAs method to save current component to a new document.

Properties are the properties of current object. Let’s take a look at features property of the Component object.

L2-Component-Feature-Property.png

We can find it has Description, Syntax, PropertyValue, Samples and Version in this document.

The document provides both samples of Python and C++. In most of the case, the Python version is very similar to the Typescript one.

If you click at C++, you’ll find following code:


#include <Fusion/Components/Component.h>

// Get the value of the property.
Ptr<Features> propertyValue = component_var->features();

All property values in the C++ are returned wrapped as a Pointer. Some of the property values will be returned as a base class(e.g., Property). You’ll need to cast them into proper types(e.g., FloatProperty).

If we don’t know the type of the property, we can use Property::objectType() and Property::claseType() to check it. For example:


std::string processProperty(const Ptr<Property>& property)
{
	const char* propertyObjectType = property->objectType();
	if (strcmp(IntegerProperty::classType(), propertyObjectType) == 0)
		return processIntegerProperty(property);
	if (strcmp(StringProperty::classType(), propertyObjectType) == 0)
		return processStringProperty(property);
	if (strcmp(BooleanProperty::classType(), propertyObjectType) == 0)
		return processBooleanProperty(property);
	if (strcmp(FloatProperty::classType(), propertyObjectType) == 0)
		return processFloatProperty(property);
	if (strcmp(ChoiceProperty::classType(), propertyObjectType) == 0)
		return processChoiceProperty(property);
	if (strcmp(AppearanceTextureProperty::classType(), propertyObjectType) == 0)
		return processAppearanceTextureProperty(property);
	if (strcmp(ColorProperty::classType(), propertyObjectType) == 0)
		return processColorProperty(property);
	if (strcmp(FilenameProperty::classType(), propertyObjectType) == 0)
		return processFilenameProperty(property);
	return std::string(", type:") + propertyObjectType;
}

It will match most of the property in the Fusion and call a method to process it.

The PropertyValue part will tell if current value is a readonly value. If it could be updated, you can pass the new value as a parameter to it.

The last part of this page is Samples this API uses and Version it is introduced. You can use samples as a reference when writing your script/add-in.

The Component object page also has two Samples and Version part. It also has two additional parts, Accessed From and Derived Class.

Derived Class is an easy way to find the derived class and cast an object to it.

Accessed From is my favorite part. It tells you how to get this object with other objects.

For example, if we want to get a Component named ‘lid’. We could begin with the Component object in the reference manual. We could find Components.itemByName in the Accessed From part.

Next, we’ll need to find the Components object. There is a Design.allComponents method to get it.

If we click at one of the samples in the Design object, we could find out that we can get the active design with Applicaton::activeProject().

Now, we have a basic concept on how to use the document, in the next lesson we’ll begin to write our first script.


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading