Calling ViewerEvent inside a custom extension

Hi @alex
In the following extension, my aim is to double-click the sketches to show/hide their dimensions. I am familiar with the show/hide part and how to find the different objects.
https://github.com/ENAC-CNPA/geometrie-pour-architectes/blob/399f919345b4d7b17f7d0391c508b3c66deb84d8/src/extensions/dimensions.ts
My problem is this: I find the objects in the following way:

const topSolidAllElements =
      this.viewer.getWorldTree().root.model.children[0].children[0].children;

This seems to give me the objects as full JS objects, and not as the viewer’s interactive objects, on which to call the “on” function. I get this error:
error = Uncaught (in promise) TypeError: sketch.on is not a function

What would be the correct way to get an object on which to call the ViewerEvent function?

Thank you very much

While it’s possible to access nodes straight from the tree like you do, it’s discouraged. Generally you want to search by specific ids, or if ids are not known, by specific property values. If you let me know how you can identify topSolidAllElements nodes I can show you how to fetch them in a more elegant way

Regarding double-clicking, I believe you misunderstood how the event works. Nodes do not emit events. The ViewerEvent you are looking for is ViewerEvent.ObjectDoubleClicked which you can listen for on the viewer instance itself. The event’s payload will be a SelectionEvent which will hold all the nodes you clicked on, ordered by distance to the camera

viewer.on(ViewerEvent.ObjectDoubleClicked, (event: SelectionEvent | null) => {
  if(!event) return
  const doubleClickedNode = event.hits[0].node
  // Do something with the node
})

Cheers

1 Like

Thanks a lot @alex !
I’m trying to implement it. Your code works really well, now I understand the concept of ViewerEvent.

I have a subsidiary question. I need the double-click to show/hide dimensions of sketches. In TopSolid, dimensions are displayed as lines and a number. The sketch is sent to speckle as one object. So the normal lines (red on the screenshot) and the dimensions lines (black on the picture) are together in the same object, as different profiles of the sketch
My question is : does filtering.hideObjects also works on parts of an object? (it seems not) Or should we send to speckle the sketch and its dimensions separately as two objects?
Thank you

Hi @RaphaelVouilloz

Glad you got it working! :winking_spockle:

Filtering works on a per-object basis so I would recommend you split the parts that you need to filter individually.

Cheers

Ok good to know! thanks @alex

1 Like