public Transform Draw(DMesh3 dmeshin, Material mat, Material Wf, bool project) { mainMat = mat; selectedMat = Wf; dmesh = dmeshin.Compactify(); MeshFilter mf = GetComponent <MeshFilter>(); MeshCollider[] mc = GetComponents <MeshCollider>(); mr = GetComponent <MeshRenderer>(); mr.material = mainMat; Mesh umesh = dmesh.ToMesh(project); umesh.RecalculateBounds(); umesh.RecalculateNormals(); umesh.RecalculateTangents(); mf.mesh = umesh; mc[0].sharedMesh = mf.sharedMesh; Mesh imesh = new Mesh(); imesh.MarkDynamic(); imesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; imesh.vertices = umesh.vertices; imesh.uv = umesh.uv; int[] t = umesh.triangles; Array.Reverse(t); imesh.triangles = t; mc[1].sharedMesh = imesh; return(transform); }
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; } }