/// <summary> /// Called to draw the line /// </summary> /// <param name="lineIn"> A LineString</param> /// <param name="symbology">The symbo,logy to be applied to the loine</param> /// <param name="LinePrefab"> The prefab to be used for the line</param> /// <param name="HandlePrefab"> The prefab to be used for the handle</param> /// <param name="LabelPrefab"> the prefab to used for the label</param> public void Draw(Geometry geom, Dictionary <string, Unit> symbology, GameObject LinePrefab, GameObject HandlePrefab, GameObject LabelPrefab, Material mainMat, Material selectedMat, Material lineMain, Material lineSelected, bool isring = false) { this.symbology = symbology; this.LinePrefab = LinePrefab; this.HandlePrefab = HandlePrefab; this.LabelPrefab = LabelPrefab; this.mainMat = mainMat; this.selectedMat = selectedMat; this.lineMain = lineMain; this.lineSelected = lineSelected; Lr = geom.IsRing(); if (isring) { Lr = true; } Vector3[] line = geom.TransformWorld(); curve.FromGeometry(geom); string type = geom.GetGeometryType().ToString(); bool IsRing = geom.IsRing(); int i = 0; foreach (Vector3 vertex in line) { if (!(i + 1 == line.Length && Lr)) { _createVertex(vertex, i); } if (i + 1 != line.Length) { _createSegment(vertex, line[i + 1], i, (i + 2 == line.Length && Lr)); } i++; } curve.Vector3(GetVertexPositions(), Lr); //Set the label if (LabelPrefab != null) { if (symbology["line"].ContainsKey("Label") && symbology["line"].Label != null && (feature?.ContainsKey(symbology["line"].Label) ?? false)) { GameObject labelObject = Instantiate(LabelPrefab, _labelPosition(), Quaternion.identity, transform); label = labelObject.transform; Text labelText = labelObject.GetComponentInChildren <Text>(); labelText.text = (string)feature.Get(symbology["line"].Label); } } }
protected override async Task _init() { RecordSet layer = _layer as RecordSet; string ex = Path.GetExtension(layer.Source).ToLower(); if (ex != ".dxf") { DMesh3Builder meshes = await loadObj(layer.Source); features = meshes.Meshes; foreach (DMesh3 mesh in features) { foreach (int idx in mesh.VertexIndices()) { Vector3d vtx = mesh.GetVertex(idx); mesh.SetVertex(idx, new Vector3d(vtx.x, vtx.z, vtx.y)); } } symbology = layer.Properties.Units; } if (ex == ".dxf") { List <Vector3d> vertexes = new List <Vector3d>(); List <Index3i> tris = new List <Index3i>(); try { // // Try opening with netDxf - this will only open files in autoCAD version 2000 or later // if (layer.Crs != null && layer.Crs != "") { SetCrs(Convert.TextToSR(layer.Crs)); } DXF.DxfDocument doc; using (Stream stream = File.Open(layer.Source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { doc = DXF.DxfDocument.Load(stream); stream.Close(); } string layout = doc.ActiveLayout; IEnumerable <Face3d> faces = doc.Faces3d; List <DCurve3> curves = new List <DCurve3>(); CoordinateTransformation transform = new CoordinateTransformation(GetCrs(), AppState.instance.mapProj); foreach (Face3d face in faces) { List <Vector3d> tri = new List <Vector3d>(); tri.Add(face.FirstVertex.ToVector3d(transform)); tri.Add(face.SecondVertex.ToVector3d(transform)); tri.Add(face.ThirdVertex.ToVector3d(transform)); if (face.FourthVertex != face.ThirdVertex) { Debug.Log(" Not a Tringle"); } curves.Add(new DCurve3(tri, false, true)); } // // for each face, check to make sure that vertices are in the vertex list and add the tri to the tri list // foreach (DCurve3 curve in curves) { List <int> tri = new List <int>(); for (int i = 0; i < 3; i++) { Vector3d v = curve.GetVertex(i); int index = vertexes.IndexOf(v); if (index == -1) { vertexes.Add(v); index = vertexes.IndexOf(v); } tri.Add(index); } tris.Add(new Index3i(tri.ToArray())); } } catch { // // if netDXF fails - try opening in GDAL that can open AutoCAD 2 file // using (OgrReader ogrReader = new OgrReader()) { await ogrReader.Load(layer.Source, layer.Properties.ReadOnly? 0 : 1, layer.Properties.SourceType); entities = ogrReader.GetLayers()[0]; SetCrs(OgrReader.getSR(entities, layer)); RecordSet metadata = GetMetadata(); if (metadata.Properties.BBox != null) { entities.SetSpatialFilterRect(metadata.Properties.BBox[0], metadata.Properties.BBox[1], metadata.Properties.BBox[2], metadata.Properties.BBox[3]); } await ogrReader.GetFeaturesAsync(entities); foreach (Feature feature in ogrReader.features) { Geometry geom = feature.GetGeometryRef(); if (geom == null) { continue; } wkbGeometryType ftype = geom.GetGeometryType(); OgrReader.Flatten(ref ftype); // // Get the faces // if (ftype == wkbGeometryType.wkbPolygon) { List <Geometry> LinearRings = new List <Geometry>(); List <DCurve3> curves = new List <DCurve3>(); for (int i = 0; i < geom.GetGeometryCount(); i++) { LinearRings.Add(geom.GetGeometryRef(i)); } // // Load the faces as a list of DCurve3 // foreach (Geometry LinearRing in LinearRings) { wkbGeometryType type = LinearRing.GetGeometryType(); if (type == wkbGeometryType.wkbLinearRing || type == wkbGeometryType.wkbLineString25D || type == wkbGeometryType.wkbLineString) { LinearRing.CloseRings(); DCurve3 curve = new DCurve3(); curve.FromGeometry(LinearRing, GetCrs()); if (curve.VertexCount != 4) { Debug.LogError("incorrect face size"); } else { curves.Add(curve); } } } // // for each tri, check to make sure that vertcie are in the vertex list and add the tri to the tri list // foreach (DCurve3 curve in curves) { List <int> tri = new List <int>(); for (int i = 0; i < 3; i++) { Vector3d v = curve.GetVertex(i); int index = vertexes.IndexOf(v); if (index == -1) { vertexes.Add(v); index = vertexes.IndexOf(v); } tri.Add(index); } tris.Add(new Index3i(tri.ToArray())); } } } } } // // vertexes and tris should now describe a mesh // DMesh3 dmesh = new DMesh3(false, false, false, false); vertexes.ForEach(v => dmesh.AppendVertex(v)); tris.ForEach(t => dmesh.AppendTriangle(t)); features = new List <DMesh3>(); features.Add(dmesh.Compactify()); symbology = layer.Properties.Units; return; } }