Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X
Dear Community
I've found some code to get the position of an assembly part here:
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
Solved! Go to Solution.
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);
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 !
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);
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 !
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
further looking in the meta .json file -the bounding box information could be found there ... here a part of blu_pump.metaddata.json:
...
...
,"Part Name":"93G824.PRT","Part Path":"Illustration/BOBCAT_S185.ASM/6725314.ASM/93G824.PRT"},"__StructureProperties":{"Bounds":{"min":[0.5037899613380432,-0.07964058220386505,0.18049299716949463],"max":[0.5228399634361267,-0.060590580105781555,0.23129300773143768]},"Location":[-1,1.2246467991473532e-16,-4.493862367303331e-16,0,-4.493862367303331e-16,-6.123233995736761e-17,1,0,1.224646799147353e-16,1,6.123233995736766e-17,0,0.513314962387085,-0.0701155811548233,0.21859300136566162,1]}},"/0/0/4":{"__PV_SystemProperties":{"Child Count":"0","Component Name":"93G824.PRT","Display Name":"93G824.PRT","Model Extents (mm)":"50.8","OL File Name":"ldemo_0_bobcat_s185_asm_181.ol","Part Depth":"4","Part ID":"4","Part ID Path":"/0/0/4","Part Name":"93G824.PRT","Part Path":"Illustration/BOBCAT_S185.ASM/6725314.ASM/93G824.PRT"},"__StructureProperties":{"Bounds":{"min":[0.6162821650505066,-0.04949839040637016,0.18049299716949463],"max":[0.6353321671485901,-0.030448388308286667,0.23129300773143768]},"Location":[-1,1.2246467991473532e-16,-4.493862367303331e-16,0,-4.493862367303331e-16,-6.123233995736761e-17,1,0,1.224646799147353e-16,1,6.123233995736766e-17,0,0.6258071660995483,-0.039973389357328415,0.21859300136566162,1]}},"/0/0/5":{"__PV_SystemProperties":{"Child Count":"0","Component Name":"93G824.PRT","Display Name":"93G824.PRT","Model Extents (mm)":"50.8","OL File Name":"ldemo_0_bobcat_s185_asm_181.ol","Part Depth":"4","Part ID":"5","Part ID Path":"/0/0/5","Part Name":"93G824.PRT","Part Path":"Illustration/BOBCAT_S185.ASM/6725314.ASM/93G824.PRT"},"__StructureProperties":{"Bounds":{"min":[0.586139976978302,0.06299380213022232,0.18049299716949463],"max":[0.6051899790763855,0.08204380422830582,0.23129300773143768]},"Location":[-1,1.2246467991473532e-16,-4.493862367303331e-16,0,-4.493862367303331e-16,-6.123233995736761e-17,1,0,1.224646799147353e-16,1,6.123233995736766e-17,0,0.5956649780273438,0.07251880317926407,0.21859300136566162,1]}},
...
...
Here example for "Part ID Path":"/0/0/5","Part Name":"93G824.PRT"
{"Bounds":{"min":[0.586139976978302,0.06299380213022232,0.18049299716949463],"max":[0.6051899790763855,0.08204380422830582,0.23129300773143768]},"Location":[-1,1.2246467991473532e-16,-4.493862367303331e-16,0,-4.493862367303331e-16,-6.123233995736761e-17,1,0,1.224646799147353e-16,1,6.123233995736766e-17,0,0.5956649780273438,0.07251880317926407,0.21859300136566162,1]}},
Just tried it in another experience, and had to upload (generate metadata.json file) the model again, to make it work. it seems like the new version of studio has a different metadata generator, which enables the bounding box function. just as info for anyone else trying to use this.