In addition to the default markup provided, you can create your own customized markup. You will need a class for your markup item that extends the Markup.MarkupItem class. Typically, you will also want an operator that can interact with the model and use mouse events to add your markup to the scene. Additionally, you may want your markup included with the default markup when exportMarkup() is called, which will require a custom markup item manager class to serialize and deserialize your markup items.
Custom Markup Item
Creating a custom markup item class that extends the markup item base class allows you to customize its behavior and appearance. Typically, markup items will be registered with the markup manager, which will manage calling the functions on the markup item like draw, remove, hit, etc.
- draw() – The draw function is called when the markup item should be redrawn. When the camera changes, the scene is redrawn, and elements in the SVG layer are removed. If the markup is drawn with SVG, the markup renderer can be used to draw the markup. The markup renderer supports drawing predefined shapes (circle, rectangle, polyline, polygon, text box, etc.).
If your markup involves non-SVG elements, you will need to manage those separately. For example, 3d geometry will not be removed when the camera changes, HTML elements may need to have position updated if the position depends on the markup location, etc.
- remove() – Any cleanup that needs to be done when the markup item is removed should be done here. To remove a markup element, remove() should not be explicitly called. Instead, the markup should be unregistered with the markup manager using the unregisterMarkup() function. For SVG elements, there is no extra cleanup required (after the markup is unregistered with the markup manager, the SVG will no longer be rendered). If there are additional non-SVG elements that require removal, you will need to manage those separately. For example, removing 3d geometry or HTML elements.
- hit() – The hit function is called with a 2d screen space coordinate to determine if the markup element is at that coordinate. You will need to implement this function to be able to select your markup. It is up to you to determine what counts as a 'hit' for your markup item. Note: The markup renderer has useful functions for determining the size of text and text boxes (measureTextBox() and measureText()).
- toJson() – The toJson() function creates a JSON object with the markup data. Only data that is required to reconstruct the markup item needs to be included in the JSON object.
Custom Markup Item Operator
Creating an operator for our markup item will allow us to place and interact with our markup in the scene. The operator will create the markup item and register it with the markup manager. If there is any interaction with the markup item after it has been created, the operator will handle that interaction as well.
Custom Markup Item ManagerA markup item manager typically will store a list of markup items created. The markup item manager can be registered with the markup manager. This allows the markup manager to include the custom markup item with the default markup items exported when exportMarkup() is called. A markup item manager needs to extend the MarkupTypeManager. The two functions that are required to be implemented are exportMarkup() and loadData().
- exportMarkup() – The exportMarkup() function creates an array of JSON objects. Each object corresponds to one markup item.
- loadData() – The loadData() function takes an array of JSON objects and uses them to reconstruct each markup item. It returns a promise with an array of Booleans indicating if each markup item was successfully constructed.
We will create a custom callout markup item. When the model is selected, a callout with the part name will display. The callout will be drawn with SVG, and consist of an arrow, a line along the selection normal, and a text box with the part name.
Create a CustomMarkupItem class that derives from the Communicator.Markup.MarkupItem class. The markup item class will store data about our markup item. We will need to implement the draw() function, which will render our markup in the scene, and the hit() function which will allow our markup to be selected.
Our callout markup item will have a line with an arrow pointing to the part that was selected and a text box containing the name of the part. The line and arrow can be drawn with a Markup.Shape.Polyline, and the textbox will use a Markup.Shape.TextBox. In the constructor, we will create the polyline and text box, and set the styles that will be used.
Add the draw function to the custom markup item class. The draw function is used to render SVG to the screen. It is called each time the markup needs to be rendered. We will use the markup managers Markup.MarkupRender to draw the SVG. The selection position is a world space coordinate, and will need to be converted to screen space to render the polyline. The box position will be calculated in world space, and also converted to screen space. Then the selection position and box position are used to update the polyline points and box position. Finally, the renderer draws the SVG.
The hit function allows the markup item to be selected. It takes a 2d screen space point and decides if the markup item has been 'hit'. In this case, we will use the bounding of the text box, and declare a hit if the point is inside.
The next step in adding the markup to the scene will be creating an operator. The operator will create the markup, and update the length of the callout as the mouse is moved. In order to calculate the new length of the callout, the operator needs to have access to the selection position and normal, and set the markup length property. Add the following getters and setters to the custom markup item.
Custom Markup Operator
Create a CustomMarkupOperator class that derives from the Communicator.Operator.Operator class.
The first function, addMarkupItem(), takes a 2d screen position coordinate, performs a selection on the model at that coordinate, and if the selection is successful, creates a callout markup at that selection position in world space. When onMouseDown is called, it will use the event parameter to get the position, and call addMarkupItem() with it. We will also need to add to class variables to keep track of the state, handled, and activeMarkupItem. The handled variable is used to prevent other operators on the stack from interacting with the scene at the same time we are dragging to create our markup item. The activeMarkupItem variable is used to keep track of the most recently created markup item.
The next function, updateActiveMarkupItem, takes a 2d screen position coordinate, and updates the markup length. To do this, it creates a line along the view plane normal at the screen position, and finds the closest point on the line from the selection position along the selection normal using the utility function lineLineIntersect. The markup length is then updated.
The onMouseDown(), onMouseMove(), and onMouseUp() functions allow an operator to interact with mouse events. We will use these functions to create our markup item on mouse down, and update it on mouse move. In onMouseDown, we see if a markup item is under the cursor. If so, we will adjust the length of the selected markup item otherwise, we will use the mouse position to add a markup item if the model selection is successful. If successful, the handled variable will be true, and activeMarkupItem will contain the created markup item. In onMouseMove, we will use the mouse position to update the length of the markup item line segment. In onMouseUp, we set the activeMarkupItem to null, as we no longer want to update it afterwards.
Create an HTML page with an instance of the viewer. Make sure to include the CustomMarkupExample.js and CustomMarkupOperator.js files.
Create an instance of our custom markup operator and push it onto the operator stack.
There are several callbacks that are triggered when markup is created, deleted, or manipulated.
Line markup items are 3d geometry that is drawn in the scene. It is exported by default with the markup manager exportMarkup() function. The following callbacks relate to line creation, deletion, and loading from exported markup data:
Measurement markup items are drawn as 2d SVG and redrawn each time the camera changes to appear 3d. The following callbacks relate to measurement creation, deletion, visibility, and updating values:
Redline markup is a 2d markup drawn as SVG that is associated with a camera and markup view. If the camera changes, any redline shown will be hidden. The following callbacks relate to redline creation, deletion, and updating position / text: