Skip to main content
15-Moonstone
November 21, 2023
Solved

Geting Model Position

  • November 21, 2023
  • 1 reply
  • 1829 views

Dear Community

 

I've found some code to get the position of an assembly part here:

https://support.ptc.com/help/vuforia/studio/en/index.html#page/Studio_Help_Center/cad_structure_api.html 

 

But it doesn't seem to work for me

 

 

$scope.centerof = function() {

// Get the Structure information for model-1
 PTC.Structure.fromId('model-1').then ( (structure) => {

 // Get the properties of the 'model-1' widget
 var widgetProps = $scope.view.wdg['model-1'];

 // Get the bounding box information for part '/0/0/2'
 var bbox = structure.getBounds('/0/0/2');

 // Transform the bounding box to account for the 'model-1' widget's location
 var xform_bbox = bbox.transform(
 [widgetProps.x, widgetProps.y, widgetProps.z],
 [widgetProps.rx, widgetProps.ry, widgetProps.rz],
 widgetProps.scale);

 // Move '3DImage-1' to the center of the bounding box
 $scope.view.wdg['3DImage-1'].x = xform_bbox.center.x;
 $scope.view.wdg['3DImage-1'].y = xform_bbox.center.y; 
 $scope.view.wdg['3DImage-1'].z = xform_bbox.center.z;

 // Get the location information for part '/0/2/8'
 var loc = structure.getLocation('/0/0/2');

 // Transform the location to account for the 'model-1' widget's location
 var xform_loc = loc.transform(
 [widgetProps.x, widgetProps.y, widgetProps.z],
 [widgetProps.rx, widgetProps.ry, widgetProps.rz],
 widgetProps.scale);

 // Get the rotational component as euler angles
 var rot = xform_loc.rotation.asEuler();

 // Rotate '3DImage-2' to match the rotation
 $scope.view.wdg['3DImage-1'].rx = rot.x;
 $scope.view.wdg['3DImage-1'].ry = rot.y; 
 $scope.view.wdg['3DImage-1'].rz = rot.z;
 });

}
 

 

I've added a demo experience to troubleshoot the issue.

 

Does anyone know what's wrong?

 

Best wishes

 

Alex

Best answer by RolandRaytchev

Hi @AlexK ,

possibly the project used old template. Also there was no meta .json file - at least I did not find such after I  imported the project to Vuforia Studio. So I  tested it ( with   Vuforia Studio 9.15)-

One point is that all the  parameter objects  should be an array from 3 items (also the scale) according to the PTC Studio Help.  so changed it e.g.:

 

 // Get the bounding box information for part '/0/0/2'
 var bbox = structure.getBounds('/0/0/2');
 console.log("bbox")
 console.warn(structure.getBounds('/0/0/2'))
 posArr=[]
 posArr.push(widgetProps.x);posArr.push(widgetProps.y);posArr.push(widgetProps.z)
 rotArr=[]
 rotArr.push(widgetProps.rx);posArr.push(widgetProps.ry);posArr.push(widgetProps.rz)
 scArr=[]
 scArr.push(widgetProps.scale);posArr.push(widgetProps.scale);posArr.push(widgetProps.scale)
 
 // Transform the bounding box to account for the 'model-1' widget's location
 var xform_bbox = bbox.transform(posArr,rotArr,scArr);

 

2023-11-21_12-31-20.jpg

So now  it seems to work. Here the modified project attached to the post.

I think to get the (new ) functionality to work you need to start  with the latest version and create a new project. I did not  see this functionality  before. Because I remember that in previous    versions we needed to use an TWX extension to extract the bbox data . I remember that such requirement was reported as enhancement to R&D Team and now is really  good to know that it is already implemented in the current functionality !

1 reply

21-Topaz I
November 21, 2023

Hi @AlexK ,

possibly the project used old template. Also there was no meta .json file - at least I did not find such after I  imported the project to Vuforia Studio. So I  tested it ( with   Vuforia Studio 9.15)-

One point is that all the  parameter objects  should be an array from 3 items (also the scale) according to the PTC Studio Help.  so changed it e.g.:

 

 // Get the bounding box information for part '/0/0/2'
 var bbox = structure.getBounds('/0/0/2');
 console.log("bbox")
 console.warn(structure.getBounds('/0/0/2'))
 posArr=[]
 posArr.push(widgetProps.x);posArr.push(widgetProps.y);posArr.push(widgetProps.z)
 rotArr=[]
 rotArr.push(widgetProps.rx);posArr.push(widgetProps.ry);posArr.push(widgetProps.rz)
 scArr=[]
 scArr.push(widgetProps.scale);posArr.push(widgetProps.scale);posArr.push(widgetProps.scale)
 
 // Transform the bounding box to account for the 'model-1' widget's location
 var xform_bbox = bbox.transform(posArr,rotArr,scArr);

 

2023-11-21_12-31-20.jpg

So now  it seems to work. Here the modified project attached to the post.

I think to get the (new ) functionality to work you need to start  with the latest version and create a new project. I did not  see this functionality  before. Because I remember that in previous    versions we needed to use an TWX extension to extract the bbox data . I remember that such requirement was reported as enhancement to R&D Team and now is really  good to know that it is already implemented in the current functionality !

AlexK15-MoonstoneAuthor
15-Moonstone
November 21, 2023

Hi Roland

 

Thank you very much, your version works great! I did have to update my studio version to make it work.

 

Best wishes

 

Alex