Skip to content

获取模型方法

获取模型方法,是为了在场景中精准的找到模型,从而对模型进行一系列操作。

初始化场景

默认已经设置了一个 div 宽度为 600px, 高度为 400px, id 设置为 TO 的容器。
且已经完成初始化导入模型操作,请参考 导入模型章节

getObjectByProperty查找模型

依据对象的属性值来查找场景中的特定对象。该方法定义于Object3D类,所以场景里的任何对象都能使用它。

js
TO.scene.getObjectByProperty(name, value, recursive);

方法参数说明

  • name:要查找的属性名称,为字符串类型
  • value:表示该属性所对应的值
  • recursive:可选参数,布尔值。若设为true(默认值),就会递归查找所有的子对象;若设为false,则只会在直接子对象中进行查找

实际使用案例

已导入ABB机械臂为例,具体代码请参考 导入模型章节

  1. 通过 name 属性查找对象
js
let model = TO.scene.getObjectByProperty("name", "WS30-1"); //找到名称为“WS30-1”的模型对象
console.log(model);
  1. 通过自定义属性查找对象
js
let model = TO.scene.getObjectByProperty("userData.by", "ThingOrigin"); //找到userData.by 为 ThingOrigin的模型对象
console.log(model);

getModelCenter获取模型中心点位置

场景中获取模型中心点位置。

js
TO.tool.getModelCenter(model);

方法参数说明

  • model:表示该模型

实际使用案例

已导入ABB机械臂为例,具体代码请参考 导入模型章节

js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let center = TO.tool.getModelCenter(model);//获取模型中心点
console.log(center);

getModelBox获取模型包围盒

获取模型包围盒。

js
TO.tool.getModelBox(model);

方法参数说明

  • model:表示该模型

实际使用案例

已导入ABB机械臂为例,具体代码请参考 导入模型章节

js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let box = TO.tool.getModelBox(model);//获取模型包围盒
console.log(box);

getModelSize获取模型包围盒尺寸

获取模型包围盒尺寸。

js
TO.tool.getModelSize(model);

方法参数说明

  • model:表示该模型

实际使用案例

已导入ABB机械臂为例,具体代码请参考 导入模型章节

js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let boxSize = TO.tool.getModelSize(model);//获取模型包围盒尺寸
console.log(boxSize);

getModelSphere获取模型的包裹球

获取模型的包裹球。

js
TO.tool.getModelSphere(model);

方法参数说明

  • model:表示该模型

实际使用案例

已导入ABB机械臂为例,具体代码请参考 导入模型章节

js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let sphere = TO.tool.getModelSphere(model);//获取模型的包裹球
console.log(sphere);

getModelInfo获取模型信息

获取模型信息。

js
TO.tool.getModelInfo(model);

方法参数说明

  • model:表示该模型

实际使用案例

已导入ABB机械臂为例,具体代码请参考 导入模型章节

js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let modelInfo = TO.tool.getModelInfo(model);//获取模型信息
console.log(modelInfo);

getChildrenInfo获取模型的子模型信息

获取模型的子模型信息。

js
TO.tool.getChildrenInfo(model);

方法参数说明

  • model:表示该模型

实际使用案例

已导入ABB机械臂为例,具体代码请参考 导入模型章节

js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let child = TO.tool.getChildrenInfo(model);//获取模型的子模型信息
console.log(child);

getWorldPosition获取模型世界坐标位置

获取模型世界坐标位置。

js
TO.tool.getWorldPosition(model);

方法参数说明

  • model:表示该模型

实际使用案例

已导入ABB机械臂为例,具体代码请参考 导入模型章节

js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let position = TO.tool.getWorldPosition(model);//获取模型世界坐标位置
console.log(position);

ownMarker获判断模型是否有2D元素

判断模型是否有2D元素。

js
TO.tool.ownMarker(model,domId);

方法参数说明

  • model:表示该模型
  • domId:表示2d元素的id

实际使用案例

已导入ABB机械臂为例,具体代码请参考 2D标签

js
let model = TO.scene.getObjectByProperty("name", "fileModel-abb"); //找到模型
let flag = TO.tool.ownMarker(model,"markerDiv");//判断模型是否有2D元素
console.log(flag);