popindoorAPI2.7.1.js 43 KB

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