popindoorAPI2.7.1.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. //V2.7.1 更改了CAD高亮HighlightSiteModelLayer,修改了带孔洞CAD的高亮效果
  2. /*3、区域模型高亮显示(改成了矢量地图,图层为room,share,balcony,重点是选中矢量面的高度设置,一定要吻合地面*/
  3. var HighlightSiteModelLayer=function (main_view,main_scene,map_view,map_scene){
  4. var geo_area = new IV.THREE.PlaneGeometry( 0.01, 0.01, 32 ); //随便创建个高亮用的网格,不创建会警告
  5. var mat_area = new IV.THREE.MeshBasicMaterial({ color: 0xffff00,transparent: true,opacity:0.3 });
  6. //mat_area.depthTest=false;
  7. var mat_room = new IV.THREE.MeshLambertMaterial({ color: 0xffff00,transparent: true,opacity:1 });
  8. var camera = main_view.getCamera();
  9. mat_area.side= IV.THREE.DoubleSide;
  10. //mat_area.depthTest=false;
  11. mat_room.side= IV.THREE.DoubleSide;
  12. mat_room.renderOrder = 2;
  13. mat_area.renderOrder = 3;
  14. //mat_room.depthTest=false;
  15. mat_room.transparent=false;
  16. mat_room.colorWrite = false;
  17. var high_light_geometry;
  18. var locked=false;
  19. var mesh_area = new IV.THREE.Mesh(geo_area,mat_area);
  20. map_scene.add(mesh_area);
  21. var geo_area_main = new IV.THREE.PlaneGeometry( 0.01, 0.01, 32 ); //随便创建个高亮用的网格,不创建会警告
  22. var mesh_area_main=new IV.THREE.Mesh(geo_area_main,mat_area);
  23. var attributes=[{name:"room",color: 0xffff00},{name:"share",color: 0x0000ff},{name:"balcony",color:0xff00ff},{name:"park",color:0x808000}];
  24. mesh_area_main.position.set( 0, 0, -4 );
  25. main_view.overlayScene.add(mesh_area_main);
  26. // console.log(mesh_area_main);
  27. var rooms,floor,squareShape,preSelectedId=-1;
  28. var wall_group = new IV.THREE.Group();
  29. main_view.overlayScene.add(wall_group);
  30. var vectorMapRepository =window.IV.injector.get("VectorMapRepository");
  31. var vectorMapService =window.IV.injector.get("VectorMapService");
  32. var geo_service = IV.injector.get('GeoTransformationService');
  33. //console.log(vectorMapService);
  34. /* 以后通过射线获取本层的实际高度,这样高亮的网格能够画再地面上
  35. setTimeout(function(){
  36. var raycastService =window.IV.injector.get("RaycastService");
  37. console.log("settimeour");
  38. var camera = IV.getMainView().getCamera();
  39. var source_position = new IV.THREE.Vector3(camera.position.x, camera.position.y, camera.position.z);
  40. var ray_position = new IV.THREE.Vector3(camera.position.x, camera.position.y, camera.position.z-0.1);
  41. var dataset=IV.getMainView().currentImage.dataset;
  42. console.log(camera.position);
  43. console.log("iv",IV.getMainView().currentImage.location); //dataset
  44. var reponse = raycastService.serverSidePickPromise(source_position, ray_position, dataset);
  45. reponse.then(function (response) { console.log(response);}).catch(function(error){
  46. console.log(error);
  47. });
  48. }, 5000);
  49. * */
  50. // console.log(floor=map_view.FloorChangerService.currentFloors[0]);
  51. //目前每层只支持单个dxf文件来高亮显示
  52. var that = this;
  53. vectorMapRepository.initialized.then(function () {
  54. floor=map_view.FloorChangerService.currentFloors[0];
  55. var vectorFiles=vectorMapRepository.data;
  56. //console.log( that);
  57. var vectorFiles_currentFloor = [];
  58. for (var i = 0; i < vectorFiles.length; i++) {
  59. var vector_zMin=vectorFiles[i].zMin,vector_zMax=vectorFiles[i].zMax;
  60. var vectorLayers=vectorFiles[i].vectorLayers;
  61. if(vector_zMin<floor.zMin&&vector_zMax>floor.zMin&&isHightLightVectorLayer(vectorLayers)) { //先判断矢量文件是不是属于这个楼层,再判断是不是有控制高亮的矢量文件
  62. //rooms=vectorLayers;
  63. vectorFiles_currentFloor.push(vectorFiles[i]);
  64. //console.log(rooms);
  65. //draw_walls(rooms,floor);
  66. //break;
  67. }
  68. }
  69. //create_menu(vectorFiles_currentFloor,that);
  70. var current_vectorFileID =0; //默认为读取的第一个文件矢量文件
  71. switchMap(vectorFiles_currentFloor,current_vectorFileID)
  72. create_menu(vectorFiles_currentFloor,that);
  73. });
  74. function switchMap(vectorFiles_currentFloor,current_vectorFileID) {
  75. var vectorFile =null;
  76. if(vectorFiles_currentFloor.length>0) {
  77. vectorFile = vectorFiles_currentFloor[current_vectorFileID];
  78. setChooseCADVisbileOnly(vectorFiles_currentFloor,current_vectorFileID); //仅对属于当前楼层的CAD文件操作,设置选中CAD可见,其他不可见
  79. // 画墙壁,起遮挡效果;
  80. rooms=vectorFile.vectorLayers;
  81. draw_walls(rooms,floor);
  82. }
  83. }
  84. function setChooseCADVisbileOnly(vectorFiles_currentFloor,current_vectorFileID) {
  85. for (var i = 0; i < vectorFiles_currentFloor.length; i++) {
  86. var vectorLayers = vectorFiles_currentFloor[i].vectorLayers;
  87. if(i === current_vectorFileID ){
  88. for (var j = 0; j < vectorLayers.length; j++) {
  89. vectorLayers[j].visible = true;
  90. }
  91. }else {
  92. for (var j = 0; j < vectorLayers.length; j++) {
  93. vectorLayers[j].visible = false;
  94. }
  95. }
  96. }
  97. vectorMapService.visibilityUpdateEvent.emit()
  98. }
  99. function create_menu(vectorFiles_currentFloor,that){
  100. var sub_menu_items = [];
  101. for (var i = 0; i < vectorFiles_currentFloor.length; i++) {
  102. var file_name = vectorFiles_currentFloor[i].name; //用文件名称来作为地图名称
  103. var map_name = file_name.substring(0,file_name.indexOf('.'));//去掉文件名称后缀
  104. var menu_item = {
  105. name:map_name,
  106. icon:"add",
  107. vid:i,
  108. callback: function (e) {
  109. IV.custom.select_map_id= e;
  110. switchMap(vectorFiles_currentFloor,sub_menu_items[e].vid)
  111. //console.log(sub_menu_items[e].vid)
  112. }
  113. };
  114. sub_menu_items.push(menu_item)
  115. }
  116. // add lock highlight
  117. that.onContextMenu = function(e) //点击测量图标执行删除功能
  118. {
  119. var menu_items=[{
  120. name: "地图模式",
  121. icon: "fa fa-list-ul",
  122. items:sub_menu_items
  123. }];
  124. if(rooms&&rooms.length>0) {
  125. // console.log(rooms.length)
  126. if (!locked) {
  127. var menu_item = {
  128. name: "区域锁定",
  129. icon: "fa-lock",
  130. vid:null,
  131. callback: function () {
  132. locked = true;
  133. }
  134. };
  135. menu_items.push(menu_item)
  136. } else {
  137. var menu_item = {
  138. name: "区域解锁",
  139. icon: "fa-unlock",
  140. vid:null,
  141. callback: function () {
  142. locked = false;
  143. }
  144. };
  145. menu_items.push(menu_item)
  146. }
  147. }
  148. return menu_items;
  149. }
  150. }
  151. IV.api.view.floorChangerService.onFloorChange.connect(floorChanged);
  152. function floorChanged(e) {
  153. ///目前每层最多只支持单个dxf文件
  154. var hasHightLightVector=false;
  155. locked=false;
  156. mesh_area.visible = false;
  157. mesh_area_main.visible = false;
  158. preSelectedId=-1;
  159. rooms=undefined;
  160. floor=map_view.FloorChangerService.currentFloors[0];
  161. var vectorFiles=vectorMapRepository.data;
  162. var vectorFiles_currentFloor = []
  163. for (var i = 0; i < vectorFiles.length; i++) {
  164. var vector_zMin=vectorFiles[i].zMin,vector_zMax=vectorFiles[i].zMax;
  165. var vectorLayers=vectorFiles[i].vectorLayers;
  166. if(vector_zMin<floor.zMin&&vector_zMax>floor.zMin&&isHightLightVectorLayer(vectorLayers)) { //先判断矢量文件是不是属于这个楼层,再判断是不是有控制高亮的矢量文件
  167. vectorFiles_currentFloor.push(vectorFiles[i]);
  168. hasHightLightVector=true;
  169. }
  170. }
  171. var current_vectorFileID =0; //默认为读取的第一个文件矢量文件
  172. switchMap(vectorFiles_currentFloor,current_vectorFileID)
  173. create_menu(vectorFiles_currentFloor,that);
  174. // for (var i = 0; i < vectorFiles.length; i++) {
  175. // var vector_zMin=vectorFiles[i].zMin,vector_zMax=vectorFiles[i].zMax;
  176. // var vectorLayers=vectorFiles[i].vectorLayers;
  177. // console.log(vectorLayers);
  178. // if(vector_zMin<floor.zMin&&vector_zMax>floor.zMin&&isHightLightVectorLayer(vectorLayers)) { //先判断矢量文件是不是属于这个楼层,再判断是不是有控制高亮的矢量文件
  179. // rooms=vectorLayers;
  180. // floor=map_view.FloorChangerService.currentFloors[0];
  181. // draw_walls(rooms,floor);
  182. // hasHightLightVector=true;
  183. // break;
  184. // }
  185. // }
  186. if(!hasHightLightVector){
  187. while (wall_group.children.length)
  188. {
  189. wall_group.remove(wall_group.children[0]);
  190. }
  191. }
  192. main_view.invalidateScene();
  193. map_view.invalidateScene();
  194. }
  195. //只要层级名称出现room,share,balcony等就说明是高亮层了
  196. function isHightLightVectorLayer(vectorLayers){
  197. //console.log(vectorLayers);
  198. for (var j = 0; j < vectorLayers.length; j++) {
  199. for (var a = 0; a < attributes.length; a++) {
  200. if (attributes[a].name === vectorLayers[j].name) {
  201. return true;
  202. }
  203. }
  204. }
  205. return false;
  206. }
  207. function draw_walls(rooms,floor){
  208. if(floor&&rooms&&rooms.length>0){ //画sitmodel 多边形
  209. var zMin=floor.zMin,zMax=floor.zMax;
  210. var height = (zMax- zMin).toFixed(2);
  211. var extrudeSettings = {
  212. depth: height,
  213. steps: 1,
  214. bevelEnabled: false,
  215. };
  216. for (var i = 0; i < rooms.length; i++) { //这里rooms其实就是层级
  217. var layer=rooms[i];
  218. if(layer.name==="room") { //车位等不是房间,不用有遮挡效果
  219. var entities=layer.vectorEntities;
  220. for (var j = 0; j < entities.length; j++) {
  221. var entity_coord = new Array();
  222. var coordinates = entities[j].vectorEntity.coordinates;
  223. for (var z = 0; z < coordinates.length; z++) {
  224. var local_coordinate = geo_service.vectorToLocal(new IV.THREE.Vector3(coordinates[z].x, coordinates[z].y, 0));
  225. //geo_service.vectorToLocal
  226. entity_coord.push(local_coordinate);
  227. }
  228. var entiryShape = new IV.THREE.Shape();
  229. entiryShape.moveTo(entity_coord[0].x, entity_coord[0].y);
  230. for (var a = 1; a < entity_coord.length; a++) {
  231. entiryShape.lineTo(entity_coord[a].x, entity_coord[a].y);
  232. }
  233. entiryShape.moveTo(entity_coord[0].x, entity_coord[0].y);
  234. var geo_room = new IV.THREE.ExtrudeBufferGeometry(entiryShape, extrudeSettings);
  235. var mesh_room = new IV.THREE.Mesh(geo_room, mat_room);
  236. mesh_room.material.needsUpdate = true;
  237. mesh_room.visible = true;
  238. mesh_room.position.set(0, 0, zMin);
  239. wall_group.add(mesh_room);
  240. }
  241. // console.log("rooms",rooms[i]);
  242. /*
  243. var varray = rooms[i].polygon.boundary;
  244. var roomShape = new IV.THREE.Shape();
  245. roomShape.moveTo(varray[0].x, varray[0].y);
  246. for (var j = 1; j < varray.length; j++) {
  247. roomShape.lineTo(varray[j].x, varray[j].y);
  248. }
  249. roomShape.lineTo(varray[0].x, varray[0].y);
  250. var geo_room = new IV.THREE.ExtrudeBufferGeometry(roomShape, extrudeSettings);
  251. var mesh_room = new IV.THREE.Mesh(geo_room, mat_room);
  252. mesh_room.material.needsUpdate = true;
  253. mesh_room.visible = true;
  254. mesh_room.position.set(0, 0, zMin);
  255. main_scene.add(mesh_room);
  256. */
  257. }
  258. }
  259. //console.log(rooms);
  260. }
  261. }
  262. this.onContextMenu=function(){
  263. if(rooms&&rooms.length>0) {
  264. if (!locked) {
  265. return {
  266. name: "区域锁定",
  267. icon: "fa-lock",
  268. callback: function () {
  269. locked = true;
  270. }
  271. };
  272. } else {
  273. return {
  274. name: "区域解锁",
  275. icon: "fa-unlock",
  276. callback: function () {
  277. locked = false;
  278. }
  279. };
  280. }
  281. }
  282. };
  283. /*
  284. this.onClick=function(){
  285. for (var i = 0; i < rooms.length; i++) {
  286. if(preSelectedId==Number(rooms[i].id)){
  287. SiteModelService.selectModel(rooms[i],false); //显示选中房间
  288. }
  289. }
  290. }
  291. */
  292. var geo_map = new IV.THREE.BufferGeometry();
  293. //var MAX_POINTS = 500;
  294. //var positions_map = new Float32Array( MAX_POINTS * 3 ); // 3 vertices per point
  295. //geo_map.addAttribute( 'position', new IV.THREE.BufferAttribute( positions_map, 3 ) );
  296. var highligh_mesh_map = new IV.THREE.Mesh( geo_map, mat_area );
  297. //highligh_mesh_map.frustumCulled = false;
  298. map_scene.add( highligh_mesh_map);
  299. var vertex2DArray = [];
  300. var face2DArray = [];
  301. var geo_main = new IV.THREE.BufferGeometry();
  302. // var positions_main = new Float32Array( MAX_POINTS * 3 ); // 3 vertices per point
  303. //geo_main.addAttribute( 'position', new IV.THREE.BufferAttribute( positions_main, 3 ) );
  304. var highligh_mesh_main = new IV.THREE.Mesh( geo_main, mat_area );
  305. highligh_mesh_main.frustumCulled = false;
  306. main_view.overlayScene.add( highligh_mesh_main);
  307. function inside_least_area(point, rooms) { //判断当前鼠标点point,是不是在多边形顶点构成的数组中
  308. var included_entirs = [];
  309. for (var i = 0; i < rooms.length; i++) { //这里rooms其实就是层级
  310. var layer = rooms[i];
  311. var entities = layer.vectorEntities;
  312. for (var j = 0; j < entities.length; j++) {
  313. var entity_coord = new Array();
  314. var coordinates = entities[j].vectorEntity.coordinates;
  315. for (var z = 0; z < coordinates.length; z++) {
  316. var local_coordinate = geo_service.vectorToLocal(new IV.THREE.Vector3(coordinates[z].x, coordinates[z].y, 0));
  317. entity_coord.push(local_coordinate);
  318. }
  319. if (inside(point, entity_coord) && entity_coord.length >= 3) {
  320. var entity_info ={};
  321. entity_info.id = entities[j].id;
  322. entity_info.name = layer.name;
  323. entity_info.array = entity_coord;
  324. included_entirs.push(entity_info)
  325. }
  326. }
  327. }
  328. return find_holes(included_entirs,rooms)
  329. }
  330. function find_center_point(array){
  331. var x=0,y=0,z=0;
  332. var l = array.length;
  333. for (var i = 0; i < l; i++) {
  334. x = x + array[i].x;
  335. y = y + array[i].y;
  336. z = z + array[i].z;
  337. }
  338. return new IV.THREE.Vector3(x/l, y/l, z/l)
  339. }
  340. function find_holes(included_entirs,rooms) {
  341. var holes = []
  342. if(included_entirs.length===1) {
  343. //var center_point = find_center_point(included_entirs[0].array)
  344. var poly= included_entirs[0].array;
  345. var poly_area = calcPolygonArea(poly);
  346. for (var i = 0; i < rooms.length; i++) { //这里rooms其实就是层级
  347. var layer = rooms[i];
  348. var entities = layer.vectorEntities;
  349. for (var j = 0; j < entities.length; j++) {
  350. var entity_coord = new Array();
  351. var coordinates = entities[j].vectorEntity.coordinates;
  352. for (var z = 0; z < coordinates.length; z++) {
  353. var local_coordinate = geo_service.vectorToLocal(new IV.THREE.Vector3(coordinates[z].x, coordinates[z].y, 0));
  354. //geo_service.vectorToLocal
  355. entity_coord.push(local_coordinate);
  356. }
  357. var center_point = find_center_point(entity_coord);
  358. var entity_area = calcPolygonArea(entity_coord);
  359. //一个多边形内又孔洞的认定条件:1/内部有重叠多边形,中心点来判断,2/重叠多边形的面积要小于当前多边形
  360. if (inside(center_point, poly) && poly_area>entity_area) {
  361. var entity_info = {};
  362. entity_info.id = entities[j].id;
  363. entity_info.name = layer.name;
  364. entity_info.array = entity_coord;
  365. holes.push(entity_info)
  366. }
  367. }
  368. }
  369. included_entirs[0].holes = holes;
  370. return included_entirs[0]
  371. }else{
  372. var min_area = 100000000;
  373. var min_index = -1;
  374. for (var i = 0; i < included_entirs.length; i++) {
  375. var current_area = calcPolygonArea(included_entirs[i].array);
  376. if(min_area > current_area ) {
  377. min_area = current_area;
  378. min_index = i;
  379. }
  380. }
  381. if (min_index !== -1 &&included_entirs[min_index].name !=="room") { //图层名称不是"room"的选择最小面积
  382. return included_entirs[min_index]
  383. }else {
  384. var entity_info = {};
  385. entity_info.id = -1;
  386. entity_info.name = "";
  387. entity_info.array = [];
  388. entity_info.holes = []
  389. return entity_info //当处于多重多边形时,就是不需要高亮
  390. }
  391. }
  392. }
  393. function calcPolygonArea(vertices) {
  394. var total = 0;
  395. for (var i = 0, l = vertices.length; i < l; i++) {
  396. var addX = vertices[i].x;
  397. var addY = vertices[i == vertices.length - 1 ? 0 : i + 1].y;
  398. var subX = vertices[i == vertices.length - 1 ? 0 : i + 1].x;
  399. var subY = vertices[i].y;
  400. total += (addX * addY * 0.5);
  401. total -= (subX * subY * 0.5);
  402. }
  403. return Math.abs(total);
  404. }
  405. function make_shape(entity_coord){
  406. var shape = new IV.THREE.Shape();
  407. if(entity_coord.length>0)
  408. shape.setFromPoints(entity_coord);
  409. return shape
  410. }
  411. function make_holes(holes){
  412. var holes_format = []
  413. for (var i = 0; i < holes.length; i++) {
  414. var hs = holes[i].array;
  415. var hole_path = new IV.THREE.Path();
  416. if(hs.length>0)
  417. hole_path.setFromPoints(hs);
  418. holes_format.push(hole_path);
  419. }
  420. return holes_format
  421. }
  422. this.onMouseMove=function(e){
  423. if(rooms&&!locked) {
  424. var p = map_view.getPositionOnGroundPlane(e.x, e.y, 0);
  425. var cursorInRoom = false;
  426. var entity = inside_least_area(p,rooms);
  427. var curr_id = entity.id;
  428. var room_name = entity.name;
  429. var entity_coord = entity.array;
  430. var entity_coord_shape = make_shape(entity_coord);
  431. if (entity_coord&&entity_coord.length&&entity_coord.length>0) {
  432. cursorInRoom = true;
  433. if (preSelectedId !== Number(curr_id)) {
  434. preSelectedId = Number(curr_id);
  435. var color=0xffa5000;
  436. for(var a=0;a<attributes.length;a++){
  437. if(attributes[a].name===room_name){
  438. color=attributes[a].color;
  439. }
  440. }
  441. if (room_name ==="room"){ //图层名称是"room"的加洞
  442. entity_coord_shape.holes = make_holes(entity.holes);
  443. }
  444. high_light_geometry = new IV.THREE.ShapeBufferGeometry( entity_coord_shape );
  445. mat_area.color.setHex(color);
  446. highligh_mesh_map.geometry=high_light_geometry;
  447. highligh_mesh_map.visible = true;
  448. highligh_mesh_main.geometry=high_light_geometry;
  449. highligh_mesh_main.visible = true;
  450. highligh_mesh_main.position.z = floor.zMin+0.2;
  451. map_view.invalidateScene();
  452. main_view.invalidateScene();
  453. }
  454. }
  455. if(!cursorInRoom&&preSelectedId !==-1){
  456. highligh_mesh_map.visible = false;
  457. highligh_mesh_main.visible = false;
  458. mesh_area.visible = false;
  459. mesh_area_main.visible = false;
  460. preSelectedId=-1;
  461. main_view.invalidateScene();
  462. map_view.invalidateScene();
  463. }
  464. }
  465. };
  466. function inside(point, vs) { //判断当前鼠标点point,是不是在多边形顶点构成的数组中
  467. var x = point.x, y = point.y;
  468. var inside = false;
  469. for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
  470. var xi = vs[i].x, yi = vs[i].y;
  471. var xj = vs[j].x, yj = vs[j].y;
  472. var intersect = ((yi > y) != (yj > y))
  473. && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
  474. if (intersect) inside = !inside;
  475. }
  476. return inside;
  477. };
  478. };
  479. /* 查各种地理坐标的功能*/
  480. var LookupGeoLocation= function (main_view,main_scene,map_view)
  481. {
  482. if(!IV.custom) //自定义功能集合
  483. IV.custom={};
  484. var geo_service = IV.injector.get('GeoTransformationService');
  485. var configService =window.IV.injector.get("ConfigService");
  486. var userService=IV.injector.get("UserService");
  487. // var geo_config={};
  488. var menuItems = IV.getMenuItems();
  489. var selected_proj4=null;
  490. var newItem = {
  491. title: "坐标查询",
  492. icon: {
  493. className: "fa fa-thumb-tack"
  494. },
  495. isVisible: function()
  496. {
  497. return true;
  498. },
  499. isPreviewIconVisible: function()
  500. {
  501. return true;
  502. },
  503. order: 40,
  504. template:findUrl()+"template_menu/PickCoordSys.html"
  505. };
  506. newItem.onExit=function(){
  507. selected_proj4=null;
  508. cursTxt.style.visibility = "hidden";
  509. };
  510. menuItems.push(newItem);
  511. var custom_config={};
  512. var proj4_setting=null;
  513. var getCustomConfig=configService.configArray.find(function(t){return t.name==='core.map.measures_save'});
  514. //configService.saveConfig('core.map.measures_save', "");
  515. if (getCustomConfig.value!=null&&getCustomConfig.value !== ""){
  516. custom_config=JSON.parse(getCustomConfig.value);
  517. if(custom_config.proj4_setting!=null&&custom_config.proj4_setting.length>0){
  518. proj4_setting=custom_config.proj4_setting;
  519. }else{
  520. default_proj4();
  521. }
  522. }else{
  523. default_proj4();
  524. }
  525. function default_proj4(){
  526. proj4_setting=[{
  527. geo_name: '本地坐标',
  528. geo_proj4: 'local'
  529. }, {
  530. geo_name: 'Wgs84坐标',
  531. geo_proj4: 'world'
  532. }, {
  533. geo_name: 'EPSG 4549:CGCS2000 / 3-degree Gauss-Kruger CM 120E',
  534. geo_proj4: '+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs'
  535. }];
  536. }
  537. IV.custom.init_coord=function(){
  538. var select_coordinate=document.getElementById("select_coordinate");
  539. var str = '<option value='+null+'>'+"无"+'</option>';
  540. for (var j = 0; j < proj4_setting.length; j++) {
  541. str +='<option value='+JSON.stringify(proj4_setting[j].geo_proj4)+'>'+proj4_setting[j].geo_name+'</option>';
  542. if(proj4_setting.geo_proj4!=="local"&&proj4_setting.geo_proj4!=="world") {
  543. proj4.defs(proj4_setting.geo_name, proj4_setting.geo_proj4);
  544. }
  545. }
  546. select_coordinate.innerHTML = str;
  547. if(!userService.isAdmin()) {
  548. var coord_div=document.getElementById("coord_editor");
  549. document.getElementById("editor_coord_froms").removeChild(coord_div);
  550. }
  551. };
  552. IV.custom.pick_coord=function(e){
  553. selected_proj4=e.options[e.selectedIndex];
  554. cursTxt.style.visibility = "hidden";
  555. // console.log(selected_proj4);
  556. };
  557. var MapCursor= function (){
  558. this.onMouseMove=function(e){
  559. if(selected_proj4&&selected_proj4.value!=="null"&&IV.getCurrentCursorPosition()&&IV.getCurrentCursorPosition().location) {
  560. var p = map_view.getPositionOnGroundPlane(e.x, e.y, 0);
  561. var offset = $("#insetPanel").position();
  562. var e_offset={x:e.x+offset.left,y:e.y+offset.top};
  563. handleCursorCoords(p,e_offset);
  564. }
  565. };
  566. this.onContextMenu = function(e)
  567. {
  568. if(selected_proj4&&selected_proj4.value!=="null"){
  569. var menu_items = [{
  570. name: "复制坐标",
  571. icon: "copy",
  572. callback: function () {
  573. setClipboardText(cursor_coordinates_forCopy);
  574. }
  575. }];
  576. return menu_items;
  577. }
  578. };
  579. }
  580. var getMapCursor = new MapCursor();
  581. map_view.addToScene(getMapCursor);
  582. var cursor_coordinates;
  583. var cursor_coordinates_forCopy;
  584. var cursTxt = document.getElementById("curscroll");
  585. this.onMouseMove=function(e){
  586. // console.log(IV.getCurrentCursorPosition());
  587. if(selected_proj4&&selected_proj4.value!=="null"&&IV.getCurrentCursorPosition()&&IV.getCurrentCursorPosition().location) {
  588. var p=IV.getCurrentCursorPosition().location;
  589. handleCursorCoords(p,e);
  590. }
  591. };
  592. function handleCursorCoords(p,e){
  593. var global = geo_service.vectorToGlobal(new IV.THREE.Vector3(p.x, p.y, p.z));
  594. if(selected_proj4.value==="local"){
  595. cursor_coordinates = " x: " + p.x.toFixed(2) + "<br/> y: " + p.y.toFixed(2) + "<br/> z: " + p.z.toFixed(2);
  596. cursor_coordinates_forCopy=" { x: " + p.x.toFixed(2) + " , y: " + p.y.toFixed(2) + " , z: " + p.z.toFixed(2)+" }";
  597. cursTxt.innerHTML = cursor_coordinates;
  598. cursTxt.style.left = (e.x + 10)+"px";
  599. }else if(selected_proj4.value==="world"){
  600. cursor_coordinates = " 经度(lon): " + global.x.toFixed(11) + "<br/> 纬度(lat): " + global.y.toFixed(11) + "<br/> 高度(h): " + global.z.toFixed(2);
  601. cursor_coordinates_forCopy=" { lon: " + global.x.toFixed(11) + " , lat: " + global.y.toFixed(11) + " , h: " + global.z.toFixed(2)+" }";
  602. cursTxt.innerHTML = cursor_coordinates;
  603. cursTxt.style.left = (e.x + 20)+"px";
  604. }else{
  605. var custom=proj4(selected_proj4.value).forward([global.x,global.y]);
  606. cursor_coordinates = " X: " + custom[0].toFixed(11) + "<br/>Y: " + custom[1].toFixed(11);
  607. cursor_coordinates_forCopy=" { x: " + custom[0].toFixed(11) + " , y: " + custom[1].toFixed(11) +" }";
  608. cursTxt.innerHTML = cursor_coordinates;
  609. cursTxt.style.left = (e.x + 10)+"px";
  610. }
  611. cursTxt.style.top = (e.y + 15)+"px";
  612. cursTxt.style.backgroundColor="white";
  613. cursTxt.style.visibility = "visible";
  614. }
  615. this.onContextMenu = function(e)
  616. {
  617. if(selected_proj4&&selected_proj4.value!=="null"){
  618. var menu_items = [{
  619. name: "复制坐标",
  620. icon: "copy",
  621. callback: function () {
  622. setClipboardText(cursor_coordinates_forCopy);
  623. }
  624. }];
  625. return menu_items;
  626. }
  627. };
  628. function setClipboardText(text){
  629. var id = "custom-clipboard-textarea-hidden-id";
  630. var existsTextarea = document.getElementById(id);
  631. if(!existsTextarea){
  632. var textarea = document.createElement("textarea");
  633. textarea.id = id;
  634. textarea.style.position = 'fixed';
  635. textarea.style.top = 0;
  636. textarea.style.left = 0;
  637. textarea.style.width = '1px';
  638. textarea.style.height = '1px';
  639. textarea.style.padding = 0;
  640. textarea.style.border = 'none';
  641. textarea.style.outline = 'none';
  642. textarea.style.boxShadow = 'none';
  643. textarea.style.background = 'transparent';
  644. document.querySelector("body").appendChild(textarea);
  645. existsTextarea = document.getElementById(id);
  646. }
  647. existsTextarea.value = text;
  648. existsTextarea.select();
  649. try {
  650. document.execCommand('copy');
  651. } catch (err) {
  652. console.log('Unable to copy.');
  653. }
  654. }
  655. IV.custom.save_coord_action=function(e){
  656. if(userService.isAdmin()) {
  657. var coord_name = document.getElementById("coord_name");
  658. var coord_proj4 = document.getElementById("coord_proj4");
  659. if (coord_name && coord_proj4) {
  660. if (!coord_name.value) {
  661. alert("坐标系名称不能为空!");
  662. return;
  663. }
  664. if (!coord_proj4.value) {
  665. alert("Proj4不能为空!");
  666. return;
  667. }
  668. var geo_coord = {
  669. geo_name: coord_name.value,
  670. geo_proj4: coord_proj4.value
  671. };
  672. try {
  673. proj4.defs(geo_coord.geo_name, geo_coord.geo_proj4);
  674. }
  675. catch (err) {
  676. alert("proj4输入错误,无法解析!") // 可执行
  677. return;
  678. }
  679. proj4_setting.push(geo_coord);
  680. coord_name.value="";
  681. coord_proj4.value="";
  682. custom_config.proj4_setting=proj4_setting;
  683. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  684. IV.custom.init_coord();
  685. }
  686. }else{
  687. alert("只有管理员权限才可以保存!");//要换成自定义对话框
  688. }
  689. };
  690. IV.custom.del_coord_action=function(e){
  691. if(userService.isAdmin()) {
  692. for (var i = 0; i < proj4_setting.length; i++) {
  693. var item =proj4_setting[i];
  694. if (item.geo_proj4 === selected_proj4.value&&item.geo_name===selected_proj4.innerText) {
  695. proj4_setting.splice(i, 1);
  696. break;
  697. // console.log("111111");
  698. }
  699. }
  700. custom_config.proj4_setting=proj4_setting;
  701. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  702. IV.custom.init_coord();
  703. }else{
  704. alert("只有管理员权限才可以删除!");//要换成自定义对话框
  705. }
  706. };
  707. };
  708. var Load3DModel= function (main_view,main_scene,map_view)
  709. {
  710. if(!IV.custom) //自定义功能集合
  711. IV.custom={};
  712. var geo_service = IV.injector.get('GeoTransformationService');
  713. var uploadService=IV.injector.get("UploadService");
  714. var configService =window.IV.injector.get("ConfigService");
  715. var userService=IV.injector.get("UserService");
  716. var sitmodelRepo = IV.api.SiteModelApiService.repository;
  717. var menuItems = IV.getMenuItems();
  718. var newItem = {
  719. title: "加载3D模型",
  720. icon: {
  721. className: "fa fa-thumb-tack"
  722. },
  723. isVisible: function()
  724. {
  725. return true;
  726. },
  727. isPreviewIconVisible: function()
  728. {
  729. return true;
  730. },
  731. order: 40,
  732. template:findUrl()+"template_menu/LoadModels.html"
  733. };
  734. menuItems.push(newItem);
  735. var custom_config={};
  736. var getCustomConfig=configService.configArray.find(function(t){return t.name==='core.map.measures_save'});
  737. //configService.saveConfig('core.map.measures_save', "");
  738. if (getCustomConfig.value!=null&&getCustomConfig.value !== ""){
  739. custom_config=JSON.parse(getCustomConfig.value);
  740. // do
  741. }else{
  742. //do
  743. }
  744. IV.custom.refModellDownload=function(e) {
  745. sitmodelRepo.findAll().then(function (buildings) {
  746. var exporter = new IV.THREE.OBJExporter();
  747. for (var i = 0; i < buildings.length; i++) {
  748. var building = buildings[i];
  749. var building_polygon = building.polygon.boundary;
  750. var floors = building.children;
  751. //console.log(building);
  752. for (var j = 0; j < floors.length; j++) {
  753. var floor = floors[j];
  754. var singleGeometry = new IV.THREE.Geometry();
  755. makeRoomsShape(singleGeometry,floor.children,floor.height);
  756. var floor_polygon;
  757. if(floor.polygon == null) {
  758. floor_polygon = building_polygon
  759. }else{
  760. floor_polygon = floor.polygon.boundary;
  761. }
  762. var mesh = makeShape(floor_polygon,floor.height);
  763. mesh.updateMatrix();
  764. singleGeometry.merge(mesh.geometry, mesh.matrix);
  765. var mat = new IV.THREE.MeshLambertMaterial({ color: 0xffff00,transparent: true,opacity:1 });
  766. var fmesh = new IV.THREE.Mesh(singleGeometry,mat);
  767. var floor_obj_name = "floor_"+floor.name+".obj";
  768. var result = exporter.parse(fmesh);
  769. download(floor_obj_name, result);
  770. }
  771. }
  772. });
  773. function makeRoomsShape(singleGeometry,children,height){
  774. for (var i = 0; i < children.length; i++) {
  775. var childs = children[i];
  776. if(childs&&childs.children) {
  777. //console.log(childs.children);
  778. for (var j = 0; j < childs.children.length; j++) {
  779. var boundary = childs.children[j].polygon.boundary;
  780. var mesh = makeShape(boundary,height);
  781. mesh.updateMatrix();
  782. singleGeometry.merge(mesh.geometry, mesh.matrix);
  783. }
  784. }
  785. }
  786. }
  787. function makeShape(boundary,height){
  788. var varray = boundary;
  789. var shape = new IV.THREE.Shape();
  790. shape.moveTo(varray[0].x, varray[0].y);
  791. for (var j = 1; j < varray.length; j++) {
  792. shape.lineTo(varray[j].x, varray[j].y);
  793. }
  794. shape.lineTo(varray[0].x, varray[0].y);
  795. var extrudeSettings = {
  796. depth: height,
  797. steps: 1,
  798. bevelEnabled: false,
  799. };
  800. var geo=new IV.THREE.ShapeGeometry( shape);
  801. // var mat = new IV.THREE.MeshLambertMaterial({ color: 0xffff00,transparent: true,opacity:1 });
  802. // var fmesh = new IV.THREE.Mesh(geo,mat);
  803. //console.log(geo)
  804. var fmesh = new IV.THREE.Mesh(geo);
  805. return fmesh;
  806. }
  807. function download(filename, text) {
  808. var pom = document.createElement('a');
  809. pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  810. pom.setAttribute('download', filename);
  811. if (document.createEvent) {
  812. var event = document.createEvent('MouseEvents');
  813. event.initEvent('click', true, true);
  814. pom.dispatchEvent(event);
  815. // console.log("12312313")
  816. }
  817. else {
  818. pom.click();
  819. }
  820. }
  821. };
  822. };
  823. /*查找本站域名,一个通用的方法*/
  824. function findUrl(){
  825. var pathArray = window.location.pathname.split( '/' );
  826. var newPath;
  827. for(var i=0;i<pathArray.length-1;i++){
  828. if(newPath)
  829. newPath+=pathArray[i]+"/";
  830. else
  831. newPath=pathArray[i]+"/";
  832. }
  833. return window.location.protocol + "//" + window.location.host +newPath;
  834. }
  835. /*所有自定义功能设置*/
  836. var Setting=function(){
  837. var custom_config={};
  838. var configService =IV.injector.get("ConfigService");
  839. var userService=IV.injector.get("UserService");
  840. var general_setting=null;
  841. var main_view = IV.getMainView();
  842. var main_scene = main_view.scene;
  843. var map_view = IV.getMapView();
  844. var map_scene = map_view.scene;
  845. var camera = main_view.getCamera();
  846. var getSettingConfig=configService.configArray.find(function(t){return t.name==='menu.settings.visible'});
  847. if(JSON.parse(getSettingConfig.value)&&userService.isAdmin()){
  848. if(!IV.custom) //自定义功能集合
  849. IV.custom={};
  850. var menuItems = IV.getMenuItems();
  851. var newItem = {
  852. title: "特色功能",
  853. icon: {
  854. className: "fa fa-cogs"
  855. },
  856. isVisible: function()
  857. {
  858. return true;
  859. },
  860. isPreviewIconVisible: function()
  861. {
  862. return true;
  863. },
  864. order: 100,
  865. template:findUrl()+"template_menu/Setting.html"
  866. };
  867. menuItems.push(newItem);
  868. }
  869. setting_init();
  870. function setting_init(){ //这个方法只执行一次
  871. var getCustomConfig=configService.configArray.find(function(t){return t.name==='core.map.measures_save'});
  872. //configService.saveConfig('core.map.measures_save', "");
  873. //console.log(getCustomConfig);
  874. if (getCustomConfig.value!=null&&getCustomConfig.value !== ""){
  875. custom_config=JSON.parse(getCustomConfig.value);
  876. if(custom_config.general_setting!=null){
  877. general_setting=custom_config.general_setting;
  878. }else{
  879. default_setting();
  880. }
  881. }else{
  882. default_setting();
  883. }
  884. load_menu();
  885. }
  886. IV.custom.settingOnload=function(){ //这个方法每次打开侧边菜单栏执行
  887. document.getElementById("setting_sitemodel").checked=general_setting.subArea;
  888. document.getElementById("setting_coord").checked=general_setting.cursorPosition;
  889. document.getElementById("setting_map_switch").checked=general_setting.switchMap;
  890. document.getElementById("setting_3dmodel").checked = general_setting.loadl3DModels;
  891. };
  892. function default_setting(){
  893. general_setting={
  894. subArea: false,
  895. cursorPosition: false,
  896. switchMap: false,
  897. loadl3DModels:false
  898. };
  899. }
  900. function load_menu(){
  901. if(general_setting.cursorPosition){
  902. var lookupGeoLocation = new LookupGeoLocation(main_view,main_scene,map_view, map_scene);
  903. main_view.addToScene(lookupGeoLocation);
  904. }
  905. if(general_setting.loadl3DModels){
  906. var load3DModel = new Load3DModel(main_view,main_scene,map_view, map_scene);
  907. main_view.addToScene(load3DModel);
  908. }
  909. // if(general_setting.switchMap){
  910. // var cadMapChnager = new CADMapChanger(map_view, map_scene);
  911. // map_view.addToScene(cadMapChnager);
  912. // }
  913. //
  914. if(general_setting.subArea){
  915. var highlightSiteModelLayer = new HighlightSiteModelLayer(main_view,main_scene,map_view, map_scene);
  916. map_view.addToScene(highlightSiteModelLayer);
  917. }
  918. }
  919. IV.custom.settingSave=function(e){
  920. general_setting.subArea=document.getElementById("setting_sitemodel").checked;
  921. general_setting.cursorPosition=document.getElementById("setting_coord").checked;
  922. general_setting.switchMap=document.getElementById("setting_map_switch").checked;
  923. general_setting.loadl3DModels=document.getElementById("setting_3dmodel").checked;
  924. custom_config.general_setting=general_setting;
  925. // console.log(custom_config);
  926. if(userService.isAdmin()) {
  927. configService.saveConfig('core.map.measures_save', JSON.stringify(custom_config));
  928. alert("请注意刷新网页后才能生效!");//要换成自定义对话框
  929. }else{
  930. alert("只有管理员权限才可以保存!");//要换成自定义对话框
  931. }
  932. };
  933. };