示例#1
0
        private SWglTFModel ConvertAssemblyToglTF(ModelDoc2 swModel)
        {
            SWglTFModel Model = new SWglTFModel();

            AssemblyDoc swAssDoc = (AssemblyDoc)swModel;

            object[] comps = swAssDoc.GetComponents(false);
            foreach (Component2 item in comps)
            {
                double[] MaterialValue = item.MaterialPropertyValues;
                if (MaterialValue == null)
                {
                    ModelDoc2 swCompModel = item.GetModelDoc2();
                    if (swCompModel != null)
                    {
                        MaterialValue = swCompModel.MaterialPropertyValues;
                    }
                }
                object[] bodys = item.GetBodies2((int)swBodyType_e.swAllBodies);
                if (bodys != null)
                {
                    foreach (Body2 swBody in bodys)
                    {
                        var bodymodel = GetglTFBodyModel(swBody);
                        if (bodymodel.BodyMaterialValue == null && MaterialValue != null)
                        {
                            bodymodel.BodyMaterialValue = MaterialValue;
                        }
                        bodymodel.SWMathTran = item.Transform2;
                        Model.BodyList.Add(bodymodel);
                    }
                }
            }
            return(Model);
        }
示例#2
0
        public SWglTFModel ConvertToglTFModel(ModelDoc2 swModel, out ErrorType errors)
        {
            SWglTFModel model = null;

            errors = ErrorType.DoNotKnow;
            ProgressStatus_event(0.01, "开始读取", ErrorType.NoErros);
            swDocumentTypes_e ModelType = (swDocumentTypes_e)swModel.GetType();

            switch (ModelType)
            {
            case swDocumentTypes_e.swDocNONE:
                errors = ErrorType.NoPartOrAssemblyDocument;
                return(null);

            case swDocumentTypes_e.swDocPART:
                ProgressStatus_event(0.02, "读取零件信息", ErrorType.NoErros);
                model = ConvertPartToglTF(swModel);
                ProgressStatus_event(0.5, "读取零件信息完成", ErrorType.NoErros);
                break;

            case swDocumentTypes_e.swDocASSEMBLY:
                ProgressStatus_event(0.02, "读取装配体信息", ErrorType.NoErros);
                model = ConvertAssemblyToglTF(swModel);
                ProgressStatus_event(0.5, "读取装配体信息完成", ErrorType.NoErros);
                break;

            case swDocumentTypes_e.swDocDRAWING:
                errors = ErrorType.NoPartOrAssemblyDocument;
                return(null);

            case swDocumentTypes_e.swDocSDM:
                errors = ErrorType.NoPartOrAssemblyDocument;
                return(null);

            case swDocumentTypes_e.swDocLAYOUT:
                errors = ErrorType.NoPartOrAssemblyDocument;
                return(null);

            case swDocumentTypes_e.swDocIMPORTED_PART:
                break;

            case swDocumentTypes_e.swDocIMPORTED_ASSEMBLY:
                break;
            }
            if (model == null)
            {
                errors = ErrorType.DoNotKnow;
            }
            return(model);
        }
示例#3
0
        private SWglTFModel ConvertPartToglTF(ModelDoc2 swModel)
        {
            SWglTFModel Model = new SWglTFModel();

            var MaterialValue = ((PartDoc)swModel).MaterialPropertyValues;

            if (MaterialValue != null)
            {
                Model.PartMaterialValue = MaterialValue;
            }
            object[] bodys = ((PartDoc)swModel).GetBodies((int)swBodyType_e.swAllBodies);
            foreach (Body2 swBody in bodys)
            {
                Model.BodyList.Add(GetglTFBodyModel(swBody));
            }
            return(Model);
        }
示例#4
0
        public List <string> SaveAs(SWglTFModel Model, string Path, string Name)
        {
            var scene = new SharpGLTF.Scenes.SceneBuilder();

            foreach (var Body in Model.BodyList)
            {
                //创建一个网格
                var Mesh = new MeshBuilder <VERTEX>("mesh");


                var material = (Body.MaterialBuilder == null ? Model.MaterialBuilder : Body.MaterialBuilder);
                if (material == null)
                {
                    material = new MaterialBuilder()
                               .WithDoubleSide(true)
                               .WithMetallicRoughnessShader()
                               .WithChannelParam("BaseColor", new Vector4(1, 0, 0, 1));
                }
                //确定材质属性
                var prim = Mesh.UsePrimitive(material
                                             );

                foreach (var face in Body.FaceList)
                {
                    foreach (var tri in face.FaceTri)
                    {
                        prim.AddTriangle(tri.a, tri.b, tri.c);
                    }
                }

                scene.AddMesh(Mesh, Body.BodyTransform);
            }


            var model = scene.ToSchema2();

            model.SaveAsWavefront(Path + "\\" + Name + ".obj");
            model.SaveGLB(Path + "\\" + Name + ".glb");
            model.SaveGLTF(Path + "\\" + Name + ".gltf");
            return(new List <string>()
            {
                Path + "\\" + Name + ".obj",
                Path + "\\" + Name + ".glb",
                Path + "\\" + Name + ".gltf"
            });
        }
示例#5
0
        public async Task <List <string> > SaveAsAsync(SWglTFModel Model, string Path, string Name)
        {
            var scene = new SharpGLTF.Scenes.SceneBuilder();

            foreach (var Body in Model.BodyList)
            {
                foreach (var face in Body.FaceList)
                {
                    var Mesh = new MeshBuilder <VERTEX>("mesh");
                    //确定材质属性
                    var prim = Mesh.UsePrimitive(
                        (Body.MaterialBuilder == null ? Model.MaterialBuilder : Body.MaterialBuilder)
                        );


                    foreach (var tri in face.FaceTri)
                    {
                        prim.AddTriangle(tri.a, tri.b, tri.c);
                    }
                    scene.AddMesh(Mesh, Body.BodyTransform);
                }
            }
            var model = scene.ToSchema2();

            model.SaveAsWavefront(Path + "\\" + Name + ".obj");
            model.SaveGLB(Path + "\\" + Name + ".glb");
            model.SaveGLTF(Path + "\\" + Name + ".gltf");
            List <string> res = new List <string>()
            {
                Path + "\\" + Name + ".obj",
                Path + "\\" + Name + ".glb",
                Path + "\\" + Name + ".gltf"
            };

            return(res);
        }