10 Jul 2024

Drag and drop with snap points

You might have seen samples of solutions that let you drag and drop additional models into the Viewer 
https://aps.autodesk.com/blog/drag-and-drop-models-viewer
https://aps.autodesk.com/blog/drag-and-drop-design-automation-inventor-sample

To make sure the components end up at the right place you might want to use snap points.
There is a sample for that too:
https://github.com/autodesk-platform-services/aps-assembly-configurator
The video in the readme shows how the sample works.
In that case you can set up the snap points inside the Viewer.

If you want to set up the snap points inside the original model and use them inside the Viewer then the following might give you an idea.

In this case we start with an Inventor model, where we place UserCoordinateSystem objects to represent the snap points' position and direction.

UCS objects in model

However, these objects are not exported to SVF so we need to copy them into iProperty entries, which do get exported.
We use VBA code (could be done by an add-in or an iLogic rule too) to copy the values of UserCoordinateSystem object's transformation matrix into iProperties:

Sub SaveConnectorsToProperties()
  Dim d As PartDocument
  Set d = ThisDocument

  Dim ps As PropertySet
  Set ps = d.FilePropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")

  Dim p As Property

  Dim u As UserCoordinateSystem
  For Each u In d.ComponentDefinition.UserCoordinateSystems
    Dim cells() As Double
    Call u.Transformation.GetMatrixData(cells)

    ' Convert translation to document units
    Dim uom As UnitsOfMeasure
    Set uom = d.UnitsOfMeasure
    cells(3) = uom.ConvertUnits(cells(3), kCentimeterLengthUnits, uom.LengthUnits)
    cells(7) = uom.ConvertUnits(cells(7), kCentimeterLengthUnits, uom.LengthUnits)
    cells(11) = uom.ConvertUnits(cells(11), kCentimeterLengthUnits, uom.LengthUnits)

    Dim strs(15) As String
    Dim i As Integer
    For i = 0 To 15
      ' Use CStr instead of just Str so that there will be a leading 0:
      ' e.g. "0.39" instead of just ".39"
      strs(i) = CStr(cells(i))
    Next
    On Error Resume Next
    Set p = ps.Item(u.Name)
    If p Is Nothing Then
      Call ps.Add("[" + Join(strs, ",") + "]", u.Name)
    Else
      p.Value = "[" + Join(strs, ",") + "]"
      Set p = Nothing
    End If
    On Error GoTo 0
  Next
End Sub

The above code generates these properties:

Snap point information in iProperties

The same properties will be accessible inside the Viewer:

snap point properties in the viewer

On the Viewer side the snapping is based on projecting all the snap points into the 2D screen space and then checking if any snap points of the model being dropped are close enough to the snap points of the models already placed inside the Viewer
It's using a modified version of this Viewer extension for identifying snaps:
https://github.com/autodesk-platform-services/aps-assembly-configurator/blob/master/server/public/scripts/extensions/connector-runtime.js
The same extension also creates an overlay inside the Viewer to visualize the snap points

The code that deals with the drag and drop part and reading the snap point properties (Connector1-3) from the SVF file are inside the html file.

The Inventor model that also includes the VBA code, the html file and the modified connector-runtime.js are inside this zip file:
https://github.com/adamenagy/Container/blob/master/drag-drop-snap.zip

Related Article