封装的PKPM BimView的方法

 封装的方法

var ObvApiWrapper;
if (!ObvApiWrapper) {
  ObvApiWrapper = {};
}
ObvApiWrapper = function(build, obvApiObj, applicationObj) {
  var self = this;
  var obvApi = obvApiObj;
  var application = applicationObj;
  var build = build; //用于加载文档和模型的对象

  self.getObvApi = function() {
    return obvApi;
  };

  //选中,展示,隔离,隐藏操作
  self.select = function(nodeIdArray) {
    this.getObvApi() && this.getObvApi().roomSelect(nodeIdArray);
  };

  self.hideAll = function() {
    this.getObvApi() && this.getObvApi().hideAll();
  };
  self.hide = function(nodeIds) {
    this.getObvApi() && this.getObvApi().hide(nodeIds);
  };

  self.show = function(nodeIds) {
    this.getObvApi() && this.getObvApi().show(nodeIds);
  };
  self.showAll = function() {
    this.getObvApi() && this.getObvApi().showAll();
  };
  self.setIsolation = function(param) {
    this.getObvApi() && this.getObvApi().setIsolation(param);
  };
  //设置颜色,恢复颜色
  self.restoreObjectsColor = function(nodeIdArray) {
    this.getObvApi() &&
      this.getObvApi().getModels().length > 0 &&
      this.getObvApi().restoreObjectsColor(nodeIdArray);
  };

  self.restoreObjectsColor = function() {
    this.getObvApi() &&
      this.getObvApi().getModels().length > 0 &&
      this.getObvApi().restoreObjectsColor();
  };
  self.setObjectsColor = function(nodeIds, r, g, b, a) {
    this.getObvApi() && this.getObvApi().setObjectsColor(nodeIds, r, g, b, a);
  };
  //设置视角
  self.fitToView = function() {
    this.getObvApi() && this.getObvApi().fitToView();
  };
  self.fitToView = function(nodeIdArray) {
    this.getObvApi() && this.getObvApi().fitToView(nodeIdArray);
  };
  self.setViewTo = function(position, target) {
    this.getObvApi() && this.getObvApi().setViewTo(position, target);
  };
  //获取模型的id,和id和name的互找
  self.getModelIds = function() {
    if (this.getObvApi()) {
      return this.getObvApi().getModelIds();
    }
  };
  self.getModelNameById = function(modelId) {
    if (this.getObvApi()) {
      var model = this.getObvApi().getModelById(modelId);
      if (model) {
        return model.name;
      }
    }
  };
  self.getModelIdByName = function(name) {
    if (this.getObvApi()) {
      return this.getObvApi().getModelIdByName(name);
    }
  };
  self.setCamera = function(camera) {
    var viewer = this.getViewer();
    new OBV.Controllers.Viewer3d.ViewController(
      viewer
    ).setViewFromCameraInfoSync(camera, true);
  };

  self.getCameraInfo = function(camera) {
    var viewer = this.getViewer();
    new OBV.Controllers.Viewer3d.ViewController(
      viewer
    ).setViewFromCameraInfoSync(camera, true);
  };
  //是否打开,不可见构件的透明化显示
  self.isOpenXRay = function(val) {
    if (self.getObvApi()) {
      if (val) {
        this.getObvApi().setXRayColor(200, 200, 200, 0.1, false);
      } else {
        this.getObvApi().restoreXRayColor();
      }
    }
  };
  self.getViewer = function() {
    return this.getObvApi().getViewer();
  };
  
  self.unloadModel = function(modelId) {
    this.getObvApi() && this.getObvApi().unloadModel(modelId);
  };
  //加载模型,外调
  self.loadDocumentByUrn = function(documentUrn, resolve) {
    build.loadDocument(application, documentUrn).then(function(obvDocument) {
      const viewer3dItem = obvDocument.getViewer3dItem(); // guid
      if (typeof resolve === "function") {
        resolve();
      }
      build
        .load3dModels(obvApi, {
          obvDocument,
          viewer3dItem
        })
        .then(function(result) {
          console.log("load3dModels", result);
        });
    });
  };
  self.getMarkerAddin = function(callback) {
    if (!self.getObvApi()) {
      return;
    }
    var addinManager = self.getObvApi().getAddinManager();
    // 插件的ID
    var markerAddinId = "OBVAddins.Marker"; //写死window["OBVAddins"].Marker.MarkerAddin.AddinId;
    // 获取插件
    var markerAddin = addinManager.getAddin(markerAddinId);
    if (!markerAddin) {
      // 插件没有加载过,需要加载到平台中
      addinManager
        .loadAddin(markerAddinId)
        .then(function() {
          self.markerAddin = addinManager.getAddin(markerAddinId);
          callback();
        })
        .catch(function() {
          if (!self.markerAddin) {
            console.error("无法加载插件:", markerAddinId);
          }
        });
    } else {
      self.markerAddin = markerAddin;
      callback();
    }
    // return markerAddin;
  };
  //功能上创建标注
  self.createMarker = function(markerItem, item, message) {
    var markerAddin = self.markerAddin;
    if (!markerAddin) {
      return;
    }
    var position;
    //构件做标注可以
    if (item == "object") {
      var bbox = this.getObvApi().getObjectsBounds(markerItem.position);
      if (!bbox) {
        return;
      }
      position = bbox.center();
    } else {
      position = markerItem.position;
    }
    var tag = markerItem;
    var anchor = " "; // 'relationItem.relation.anchor'
    var id = new Date().getTime() + Math.random() * 100; //relationItem.relation.id + ''
    markerAddin.createTextSymbolMarker(
      position,
      anchor,
      tag,
      message,
      id,
      1,
      1
    );
  };
  //清空标注
  self.clearMarkers = function() {
    var markerAddin = self.markerAddin;
    if (!markerAddin) {
      return;
    }
    markerAddin.clearAnnotations();
  };
  //标注的点击事件
  self.getMarkerAnnotationClickedEventListener = function(resolve) {
    var markerAddin = self.markerAddin;
    markerAddin.addEventListener(
      window["OBVAddins"].Marker.MarkerEventTypes.ANNOTATION_CLICKED,
      annotation => {
        let nodeIdArray = annotation.data.tag.position;
        self.getObvApi().restoreObjectsColor();
        //  self.getObvApi().setObjectsColor(nodeIdArray, 56, 184, 49, 1);
        self.getObvApi().setObjectsColor(nodeIdArray, 255, 0, 0, 1);
        if (typeof resolve === "function") {
          resolve(annotation.data);
        }
      }
    );
  };

  //标注的点击事件
  self.getMarkerSelectedEventListener = function(resolve) {
    var markerAddin = self.markerAddin;
    markerAddin.addEventListener(
      window["OBVAddins"].Marker.MarkerEventTypes.MARKER_SELECTED,
      annotation => {
        let nodeIdArray = annotation.data.tag.position;
        self.getObvApi().restoreObjectsColor();
        // self.getObvApi().setObjectsColor(nodeIdArray, 56, 184, 49, 1);
        self.getObvApi().setObjectsColor(nodeIdArray, 255, 0, 0, 1);
        if (typeof resolve === "function") {
          resolve(annotation.data);
        }
      }
    );
  };
  /**
   * // 根据guid 获得 nodeId
   * @param {OBVAPI} obvApi
   * @param {*成功后的回调} successCallback
   * @param {*失败后的回调} errorCallback
   * @param {*modelId} modelId
   */
  self.getMapGuid = function(successCallback, errorCallback) {
    if (!this.getObvApi()) {
      return;
    }
    var modelIds = this.getObvApi().getModelIds();
    var obvApi = this.getObvApi();
    var obj = {};
    var mapIndex = 0;
    modelIds.forEach(function(modelId) {
      getObjectTree(
        obvApi,
        modelId,
        function(mapData) {
          for (var key in mapData) {
            obj[key] = mapData[key];
          }
          mapIndex++;
          if (mapIndex === modelIds.length) {
            successCallback(obj);
          }
        },
        function() {
          errorCallback(obj);
        }
      );
    });
  };
  //通过一个modelId找到树以及guid的映射关系
  function getObjectTree(obvApi, modelId, successCallback, errorCallback) {
    obvApi.getObjectTree(modelId, function(objTree) {
      var mapGuid = {};
      var modelId = objTree.modelId;
      var nodeIdArray = objTree.dbIdToNode;
      var nodeIdArrayLastIndex = nodeIdArray.length - 1;
      nodeIdArray.forEach(function(item, arrIndex) {
        // console.log("indexArr: ", indexarr);
        var nodeId = {
          dbId: item.dbId,
          modelId: modelId,
          hasChildren: !!item.children
        };
        // var nodeId = {dbId: item.dbId, modelId: modelId}
        obvApi.getProperties(
          nodeId,
          function(result) {
            for (var i = 0; i < result.properties.length; i++) {
              var name = result.properties[i].displayName;
              var val = result.properties[i].displayValue;

              if (name === "Guid" || name === "OBVID") {
                mapGuid[val] = nodeId;
                break;
              }
            }
            if (arrIndex === nodeIdArrayLastIndex) {
              successCallback(mapGuid);
            }
          },
          function(error) {
            if (arrIndex === nodeIdArrayLastIndex) {
              successCallback(mapGuid);
            }
          }
        );
      });
    });
  }
};

  

posted @ 2019-09-19 19:17  FeelRose  阅读(472)  评论(0编辑  收藏  举报