美文网首页
四.获取SolidWorks中的材质信息

四.获取SolidWorks中的材质信息

作者: dududuwei | 来源:发表于2019-11-27 22:21 被阅读0次

    四.获取SolidWorks中的材质信息

    SolidWorks插件-将SolidWorks文件转换为gltf格式

    4.1 关于材质

    其实在上一节的代码里已经有获取SolidWorks的材质信息。在SolidWorks中的零件文档有材料信息,实体(Body)有材质信息,面有材质信息。此处的材质信息包含颜色,光泽度等等。
    处理这些材质信息后才能让转换出来的文档和SolidWorks中的文档看起来相似。

    4.2 材质信息

    • 在SolidWorks中,IPartDoc,IFace2,IBody都具有MaterialPropertyValue的属性。这个属性会返回一个数组。
    [ R, G, B, Ambient, Diffuse, Specular, Shininess, Transparency, Emission ]
    
    • 在这里我们使用一个SharpglTF中MaterialBulider对象来构造一个的材质对象
     public MaterialBuilder MaterialBuilder
            {
                get
                {
                    
                        if (FaceMaterialValue != null)
                        {
                            _MaterialBuilder = new MaterialBuilder()
                            .WithDoubleSide(true)
                            .WithChannelParam(KnownChannels.Normal, new System.Numerics.Vector4(
                               Convert.ToSingle(FaceMaterialValue[0]),
                               Convert.ToSingle(FaceMaterialValue[1]),
                               Convert.ToSingle(FaceMaterialValue[2]),
                               1));
                        }
                    
                    return _MaterialBuilder;
                }
            }
    
    4.3 存储零件,实体,面的材质和几何信息
    namespace DuSwToglTF.Model
    {
        using VERTEX = SharpGLTF.Geometry.VertexTypes.VertexPosition;
    
        public class SWglTFModel
        {
            /// <summary>
            /// SolidWorks材料属性
            /// </summary>
            public double[] PartMaterialValue;
            /// <summary>
            /// Solidworks变换矩阵
            /// </summary>
            public MathTransform SWMathTran;
            /// <summary>
            /// 默认材料属性
            /// </summary>
            private MaterialBuilder InitMaterial = new MaterialBuilder()
                    .WithDoubleSide(true)
                    .WithMetallicRoughnessShader()
                    .WithChannelParam(KnownChannels.MetallicRoughness, new Vector4(1, 0, 0, 1));
            /// <summary>
            /// 初始原点矩阵
            /// </summary>
            public  Matrix4x4 InitPartPos = new Matrix4x4(
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0,1
                );
    
            /// <summary>
            /// 零件矩阵
            /// </summary>
            public Matrix4x4 PartTransform
            {
                get {
                    if (SWMathTran == null)
                    {
                        return InitPartPos;
                    }
                    else
                    {
                        double[] Tran = SWMathTran.ArrayData;
                        return new Matrix4x4(
                            Convert.ToSingle(Tran[0]), Convert.ToSingle(Tran[1]), Convert.ToSingle(Tran[2]), 0,
                            Convert.ToSingle(Tran[3]), Convert.ToSingle(Tran[4]), Convert.ToSingle(Tran[5]), 0,
                            Convert.ToSingle(Tran[6]), Convert.ToSingle(Tran[7]), Convert.ToSingle(Tran[8]), 0,
                            Convert.ToSingle(Tran[9]), Convert.ToSingle(Tran[10]), Convert.ToSingle(Tran[11]), 0
                            );
                    }
                }
            }
         
            //[ R, G, B, Ambient, Diffuse, Specular, Shininess, Transparency, Emission ]
            /// <summary>
            /// 材料属性
            /// </summary>
            public MaterialBuilder MaterialBuilder
            {
                get
                {
    
                    if (PartMaterialValue != null)
                    {
                        //if (PartMaterialValue[0] == 1 && PartMaterialValue[1] == 1 && PartMaterialValue[2] == 1)
                        //{
                        //    return InitMaterial;
                        //}
                        //else
                        //{
                        _MaterialBuilder = new MaterialBuilder()
                  .WithDoubleSide(true)
                  .WithChannelParam(KnownChannels.BaseColor, new System.Numerics.Vector4(
                     Convert.ToSingle(PartMaterialValue[0]),
                     Convert.ToSingle(PartMaterialValue[1]),
                     Convert.ToSingle(PartMaterialValue[2]),
                     1));
                        // }
                        
                    }
                    if (_MaterialBuilder == null)
                    {
                        _MaterialBuilder = InitMaterial;
                    }
                    return _MaterialBuilder;
                }
            }
            /// <summary>
            /// 所有的实体
            /// </summary>
            public List<BodyglTFModel> BodyList = new List<BodyglTFModel>();
            #region 私有字段
            private Matrix4x4 _PartTransform;
            private MaterialBuilder _MaterialBuilder = null;
            #endregion
        }
        /// <summary>
        /// 实体模型属性
        /// </summary>
        public class BodyglTFModel
        {
            /// <summary>
            /// 初始原点矩阵
            /// </summary>
            public Matrix4x4 InitPartPos = new Matrix4x4(
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1
                );
            public MathTransform SWMathTran;
    
            /// <summary>
            /// 零件矩阵
            /// </summary>
            public Matrix4x4 BodyTransform
            {
                get
                {
                    if (SWMathTran == null)
                    {
                        return InitPartPos;
                    }
                    else
                    {
                        double[] Tran = SWMathTran.ArrayData;
                        return new Matrix4x4(
                            Convert.ToSingle(Tran[0]), Convert.ToSingle(Tran[1]), Convert.ToSingle(Tran[2]), 0,
                            Convert.ToSingle(Tran[3]), Convert.ToSingle(Tran[4]), Convert.ToSingle(Tran[5]), 0,
                            Convert.ToSingle(Tran[6]), Convert.ToSingle(Tran[7]), Convert.ToSingle(Tran[8]), 0,
                            Convert.ToSingle(Tran[9]), Convert.ToSingle(Tran[10]), Convert.ToSingle(Tran[11]), 0
                            );
                    }
                }
            }
    
            /// <summary>
            /// 实体材料属性
            /// </summary>
            public double[] BodyMaterialValue;
    
            //[ R, G, B, Ambient, Diffuse, Specular, Shininess, Transparency, Emission ]
            public MaterialBuilder MaterialBuilder
            {
                get
                {
                 
                        if (BodyMaterialValue != null)
                        {
                            _MaterialBuilder = new MaterialBuilder()
                            .WithDoubleSide(true)
                            .WithChannelParam(KnownChannels.Normal, new System.Numerics.Vector4(
                               Convert.ToSingle(BodyMaterialValue[0]),
                               Convert.ToSingle(BodyMaterialValue[1]),
                               Convert.ToSingle(BodyMaterialValue[2]),
                               1));
                        }
                  
                        return _MaterialBuilder;
                    
    
                }
            }
            /// <summary>
            /// 实体中所有的面
            /// </summary>
            public List<FaceglTFModel> FaceList = new List<FaceglTFModel>();
    
            private MaterialBuilder _MaterialBuilder = null;
    
        }
    
        public class FaceglTFModel
        {
            /// <summary>
            /// 面材质属性
            /// </summary>
            public double[] FaceMaterialValue;
    
            private MaterialBuilder _MaterialBuilder = null;
            //[ R, G, B, Ambient, Diffuse, Specular, Shininess, Transparency, Emission ]
            public MaterialBuilder MaterialBuilder
            {
                get
                {
                    
                        if (FaceMaterialValue != null)
                        {
                            _MaterialBuilder = new MaterialBuilder()
                            .WithDoubleSide(true)
                            .WithChannelParam(KnownChannels.Normal, new System.Numerics.Vector4(
                               Convert.ToSingle(FaceMaterialValue[0]),
                               Convert.ToSingle(FaceMaterialValue[1]),
                               Convert.ToSingle(FaceMaterialValue[2]),
                               1));
                        }
                    
                    return _MaterialBuilder;
                }
            }
            /// <summary>
            /// 面中所有的三角面
            /// </summary>
            public List<FaceVertexModel> FaceTri = new List<FaceVertexModel>();
        }
        /// <summary>
        /// 一个三角形面的三个顶点
        /// </summary>
        public class FaceVertexModel
        {
            public VERTEX a;
            public VERTEX b;
            public VERTEX c;
        }
    }
    
    • 我们用这样一个数据结构来描述一个SolidWorks零件或者转配体。在转换中将其转换为SharpglTF的一个sence对象,在输出为gltf格式。

    相关文章

      网友评论

          本文标题:四.获取SolidWorks中的材质信息

          本文链接:https://www.haomeiwen.com/subject/vugmwctx.html