popindoorAPI2.7.1.js 43 KB

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