custom.js 43 KB

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