diff --git a/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs b/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs index 70af34200..1d012e822 100644 --- a/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs +++ b/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs @@ -1047,6 +1047,9 @@ private CadTemplate readUnlistedType(short classNumber) case "LWPOLYLINE": template = this.readLWPolyline(); break; + case "MATERIAL": + template = this.readMaterial(); + break; case "MESH": template = this.readMesh(); break; @@ -5235,6 +5238,24 @@ private CadTemplate readLWPolyline() return template; } + private CadTemplate readMaterial() + { + Material material = new Material(); + CadMaterialTemplate template = new CadMaterialTemplate(material); + + this.readCommonNonEntityData(template); + + material.Name = this._mergedReaders.ReadVariableText(); + material.Description = this._mergedReaders.ReadVariableText(); + +#if TEST + var obj = DwgStreamReaderBase.Explore(this._objectReader); + var text = DwgStreamReaderBase.Explore(this._textReader); +#endif + + return null; + } + private CadTemplate readHatch() { Hatch hatch = new Hatch(); diff --git a/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs b/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs index 735d046e2..0441d9de9 100644 --- a/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs +++ b/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs @@ -2,6 +2,7 @@ using ACadSharp.Objects; using ACadSharp.Objects.Evaluations; using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using static ACadSharp.IO.Templates.CadEvaluationGraphTemplate; @@ -74,6 +75,8 @@ private CadTemplate readObject() return this.readObjectCodes(new CadGroupTemplate(), this.readGroup); case DxfFileToken.ObjectGeoData: return this.readObjectCodes(new CadGeoDataTemplate(), this.readGeoData); + case DxfFileToken.ObjectMaterial: + return this.readObjectCodes(new CadMaterialTemplate(), this.readMaterial); case DxfFileToken.ObjectScale: return this.readObjectCodes(new CadTemplate(new Scale()), this.readScale); case DxfFileToken.ObjectTableContent: @@ -362,6 +365,96 @@ private bool readGeoData(CadTemplate template, DxfMap map) } } + private bool readMaterial(CadTemplate template, DxfMap map) + { + CadMaterialTemplate tmp = template as CadMaterialTemplate; + List arr = null; + + switch (this._reader.Code) + { + case 43: + arr = new(); + for (int i = 0; i < 16; i++) + { + Debug.Assert(this._reader.Code == 43); + + arr.Add(this._reader.ValueAsDouble); + + this._reader.ReadNext(); + } + + tmp.CadObject.DiffuseMatrix = new CSMath.Matrix4(arr.ToArray()); + return this.checkObjectEnd(template, map, this.readMaterial); + case 47: + arr = new(); + for (int i = 0; i < 16; i++) + { + Debug.Assert(this._reader.Code == 47); + + arr.Add(this._reader.ValueAsDouble); + + this._reader.ReadNext(); + } + + tmp.CadObject.SpecularMatrix = new CSMath.Matrix4(arr.ToArray()); + return this.checkObjectEnd(template, map, this.readMaterial); + case 49: + arr = new(); + for (int i = 0; i < 16; i++) + { + Debug.Assert(this._reader.Code == 49); + + arr.Add(this._reader.ValueAsDouble); + + this._reader.ReadNext(); + } + + tmp.CadObject.ReflectionMatrix = new CSMath.Matrix4(arr.ToArray()); + return this.checkObjectEnd(template, map, this.readMaterial); + case 142: + arr = new(); + for (int i = 0; i < 16; i++) + { + Debug.Assert(this._reader.Code == 142); + + arr.Add(this._reader.ValueAsDouble); + + this._reader.ReadNext(); + } + + tmp.CadObject.OpacityMatrix = new CSMath.Matrix4(arr.ToArray()); + return this.checkObjectEnd(template, map, this.readMaterial); + case 144: + arr = new(); + for (int i = 0; i < 16; i++) + { + Debug.Assert(this._reader.Code == 144); + + arr.Add(this._reader.ValueAsDouble); + + this._reader.ReadNext(); + } + + tmp.CadObject.BumpMatrix = new CSMath.Matrix4(arr.ToArray()); + return this.checkObjectEnd(template, map, this.readMaterial); + case 147: + arr = new(); + for (int i = 0; i < 16; i++) + { + Debug.Assert(this._reader.Code == 147); + + arr.Add(this._reader.ValueAsDouble); + + this._reader.ReadNext(); + } + + tmp.CadObject.RefractionMatrix = new CSMath.Matrix4(arr.ToArray()); + return this.checkObjectEnd(template, map, this.readMaterial); + default: + return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[tmp.CadObject.SubclassMarker]); + } + } + private bool readScale(CadTemplate template, DxfMap map) { switch (this._reader.Code) diff --git a/src/ACadSharp/IO/Templates/CadMaterialTemplate.cs b/src/ACadSharp/IO/Templates/CadMaterialTemplate.cs new file mode 100644 index 000000000..3aa6e63e3 --- /dev/null +++ b/src/ACadSharp/IO/Templates/CadMaterialTemplate.cs @@ -0,0 +1,16 @@ +using ACadSharp.Objects; + +namespace ACadSharp.IO.Templates +{ + internal class CadMaterialTemplate : CadTemplate + { + public CadMaterialTemplate() : base(new Material()) { } + + public CadMaterialTemplate(Material material) : base(material) { } + + public override void Build(CadDocumentBuilder builder) + { + base.Build(builder); + } + } +} diff --git a/src/ACadSharp/Objects/Material.cs b/src/ACadSharp/Objects/Material.cs index c993678d7..7e4e2e12d 100644 --- a/src/ACadSharp/Objects/Material.cs +++ b/src/ACadSharp/Objects/Material.cs @@ -1,5 +1,68 @@ -namespace ACadSharp.Objects +using ACadSharp.Attributes; +using CSMath; +using CSUtilities.Extensions; + +namespace ACadSharp.Objects { + public enum ColorMethod + { + Current = 0, + Override = 1, + } + + public enum MapSource + { + UseCurrentScene = 0, + UseImageFile = 1, + } + + public enum ProjectionMethod + { + None = 0, + Planar = 1, + Box = 2, + Cylinder = 3, + Sphere = 4 + } + + public enum TilingMethod + { + None = 0, + Tile = 1, + Crop = 2, + Clamp = 3 + } + + [System.Flags] + public enum AutoTransformMethodFlags + { + /// + /// None. + /// + None = 0, + /// + /// No auto transform. + /// + NoAutoTransform = 1, + /// + /// Scale mapper to current entity extents; translate mapper to entity origin. + /// + ScaleMapper = 2, + /// + /// Include current block transform in mapper transform. + /// + IncludeCurrentBlock = 4 + } + + /// + /// Represents a object + /// + /// + /// Object name
+ /// Dxf class name + ///
+ [DxfName(DxfFileToken.ObjectMaterial)] + [DxfSubClass(DxfSubclassMarker.Material)] public class Material : NonGraphicalObject { /// @@ -11,385 +74,427 @@ public class Material : NonGraphicalObject /// public override string SubclassMarker => DxfSubclassMarker.Material; - //1 Material name(string) - - //2 Description(string, default null string) - - //70 - - //Ambient color method(default = 0) : - - //0 = Use current color - - //1 = Override current color - - //40 - - //Ambient color factor(real, default = 1.0; valid range is 0.0 to 1.0) - - //90 - - //Ambient color value(unsigned 32-bit integer representing an AcCmEntityColor) - - //71 - - //Diffuse color method(default = 0) : - - //0 = Use current color - - //1 = Override current color - - //41 - - //Diffuse color factor(real, default = 1.0; valid range is 0.0 to 1.0) - - //91 - - //Diffuse color value(unsigned 32-bit integer representing an AcCmEntityColor) - - //42 - - //Diffuse map blend factor(real, default = 1.0) - - //72 - - //Diffuse map source(default = 1) : - - //0 = Use current scene - - //1 = Use image file(specified by file name; null file name specifies no map) - - //3 - - //Diffuse map file name(string, default = null string) - - //73 - - //Projection method of diffuse map mapper(default = 1): - - //1 = Planar - - //2 = Box - - //3 = Cylinder - - //4 = Sphere - - //74 - - //Tiling method of diffuse map mapper(default = 1): - - //1 = Tile - - //2 = Crop - - //3 = Clamp - - //75 - - //Auto transform method of diffuse map mapper(bitset, default = 1) : - - //1= No auto transform - - //2 = Scale mapper to current entity extents; translate mapper to entity origin - - //4 = Include current block transform in mapper transform - - //43 - - //Transform matrix of diffuse map mapper(16 reals; row major format; default = identity matrix) - - //44 - - //Specular gloss factor(real, default = 0.5) - - //76 - - //Specular color method(default = 0) : - - //0 = Use current color - - //1 = Override current color - - //45 - - //Specular color factor(real, default = 1.0; valid range is 0.0 to 1.0) - - //92 - - //Specular color value(unsigned 32-bit integer representing an AcCmEntityColor) - - //46 - - //Specular map blend factor(real; default = 1.0) - - //77 - - //Specular map source(default = 1) : - - //0 = Use current scene - - //1 = Use image file(specified by file name; null file name specifies no map) - - //4 - - //Specular map file name(string; default = null string) - - //78 - - //Projection method of specular map mapper(default = 1): - - //1 = Planar - - //2 = Box - - //3 = Cylinder - - //4 = Sphere - - //79 - - //Tiling method of specular map mapper(default = 1): - - //1 = Tile - - //2 = Crop - - //3 = Clamp - - //170 - - //Auto transform method of specular map mapper(bitset; default = 1): - - //1 = No auto transform - - //2 = Scale mapper to current entity extents; translate mapper to entity origin - - //4 = Include current block transform in mapper transform - - //47 - - //Transform matrix of specular map mapper(16 reals; row major format; default = identity matrix) - - //48 - - //Blend factor of reflection map(real, default = 1.0) - - //171 - - //Reflection map source(default = 1) : - - //0 = Use current scene - - //1 = Use image file(specified by file name; null file name specifies no map) - - //6 - - //Reflection map file name(string; default = null string) - - //172 - - //Projection method of reflection map mapper(default = 1): - - //1 = Planar - - //2 = Box - - //3 = Cylinder - - //4 = Sphere - - //173 - - //Tiling method of reflection map mapper(default = 1): - - //1 = Tile - - //2 = Crop - - //3 = Clamp - - //174 - - //Auto transform method of reflection map mapper(bitset; default = 1): - - //1 = No auto transform - - //2 = Scale mapper to current entity extents; translate mapper to entity origin - - //4 = Include current block transform in mapper transform - - //49 - - //Transform matrix of reflection map mapper(16 reals; row major format; default = identity matrix) - - //140 - - //Opacity percent(real; default = 1.0) - - //141 - - //Blend factor of opacity map(real; default = 1.0) - - //175 - - //Opacity map source(default = 1) : - - //0 = Use current scene - - //1 = Use image file(specified by file name; null file name specifies no map) - - //7 - - //Opacity map file name(string; default = null string) - - //176 - - //Projection method of opacity map mapper(default = 1): - - //1 = Planar - - //2 = Box - - //3 = Cylinder - - //4 = Sphere - - //177 - - //Tiling method of opacity map mapper(default = 1): - - //1 = Tile - - //2 = Crop - - //3 = Clamp - - //178 - - //Auto transform method of opacity map mapper(bitset; default = 1): - - //1 = No auto transform - - //2 = Scale mapper to current entity extents; translate mapper to entity origin - - //4 = Include current block transform in mapper transform - - //142 - - //Transform matrix of opacity map mapper(16 reals; row major format; default = identity matrix) - - //143 - - //Blend factor of bump map(real; default = 1.0) - - //179 - - //Bump map source(default = 1) : - - //0 = Use current scene - - //1 = Use image file(specified by file name; null file name specifies no map) - - //8 - - //Bump map file name(string; default = null string) - - //270 - - //Projection method of bump map mapper(default = 1): - - //1 = Planar - - //2 = Box - - //3 = Cylinder - - //4 = Sphere - - //271 - - //Tiling method of bump map mapper(default = 1): - - //1 = Tile - - //2 = Crop - - //3 = Clamp - - //272 - - //Auto transform method of bump map mapper(bitset; default = 1): - - //1 = No auto transform - - //2 = Scale mapper to current entity extents; translate mapper to entity origin - - //4 = Include current block transform in mapper transform - - //144 - - //Transform matrix of bump map mapper(16 reals; row major format; default = identity matrix) - - //145 - - //Refraction index(real; default = 1.0) - - //146 - - //Blend factor of refraction map(real; default = 1.0) - - //273 - - //Refraction map source(default = 1) : - - //0 = Use current scene - - //1 = Use image file(specified by file name; null file name specifies no map) - - //9 - - //Refraction map file name(string; default = null string) - - //274 - - //Projection method of refraction map mapper(default = 1): - - //1 = Planar - - //2 = Box - - //3 = Cylinder - - //4 = Sphere - - //275 - - //Tiling method of refraction map mapper(default = 1): - - //1 = Tile - - //2 = Crop - - //3 = Clamp - - //276 - - //Auto transform method of refraction map mapper(bitset; default = 1): - - //1 = No auto transform - - //2 = Scale mapper to current entity extents; translate mapper to entity origin - - //4 = Include current block transform in mapper transform - - //147 - - //Transform matrix of refraction map mapper(16 reals; row major format; default = identity matrix) + /// + /// Material name. + /// + [DxfCodeValue(1)] + public override string Name + { + get + { + return base.Name; + } + set + { + base.Name = value; + } + } + + /// + /// Material description. + /// + [DxfCodeValue(2)] + public string Description { get; set; } + + /// + /// Ambient color method. + /// + [DxfCodeValue(70)] + public ColorMethod AmbientColorMethod { get; set; } = ColorMethod.Current; + + /// + /// Ambient color factor. + /// + /// + /// valid range is 0.0 to 1.0) + /// + [DxfCodeValue(40)] + public double AmbientColorFactor + { + get { return this._ambientColorFactor; } + set + { + ObjectExtensions.InRange(value, 0, 1); + this._ambientColorFactor = value; + } + } + + private double _ambientColorFactor = 1.0; + + /// + /// Ambient color value. + /// + [DxfCodeValue(90)] + public Color AmbientColor { get; set; } + + /// + /// Ambient color method. + /// + [DxfCodeValue(71)] + public ColorMethod DiffuseColorMethod { get; set; } = ColorMethod.Current; + + /// + /// Diffuse color factor. + /// + /// + /// valid range is 0.0 to 1.0) + /// + [DxfCodeValue(41)] + public double DiffuseColorFactor + { + get { return this._diffuseColorFactor; } + set + { + ObjectExtensions.InRange(value, 0, 1); + this._diffuseColorFactor = value; + } + } + + private double _diffuseColorFactor = 1.0; + + /// + /// Diffuse color value. + /// + [DxfCodeValue(91)] + public Color DiffuseColor { get; set; } + + /// + /// Diffuse map blend factor. + /// + [DxfCodeValue(42)] + public double DiffuseMapBlendFactor { get; set; } = 1.0; + + /// + /// Diffuse map source. + /// + [DxfCodeValue(72)] + public MapSource DiffuseMapSource { get; set; } = MapSource.UseImageFile; + + /// + /// Diffuse map file name. + /// + /// + /// null file name specifies no map. + /// + [DxfCodeValue(3)] + public string DiffuseMapFileName { get; set; } + + /// + /// Projection method of diffuse map mapper. + /// + [DxfCodeValue(73)] + public ProjectionMethod DiffuseProjectionMethod { get; set; } = ProjectionMethod.Planar; + + /// + /// Tiling method of diffuse map mapper. + /// + [DxfCodeValue(74)] + public TilingMethod DiffuseMapper { get; set; } = TilingMethod.Tile; + + /// + /// Auto transform method of diffuse map mapper. + /// + [DxfCodeValue(75)] + public AutoTransformMethodFlags DiffuseAutoTransform { get; set; } = AutoTransformMethodFlags.NoAutoTransform; + + /// + /// Transform matrix of diffuse map mapper. + /// + [DxfCodeValue(43)] + public Matrix4 DiffuseMatrix { get; set; } = Matrix4.Identity; + + /// + /// Specular gloss factor. + /// + /// + /// default = 0.5 + /// + [DxfCodeValue(44)] + public double SpecularGlossFactor { get; set; } = 0.5; + + /// + /// Specular color method. + /// + [DxfCodeValue(76)] + public ColorMethod SpecularColorMethod { get; set; } = ColorMethod.Current; + + /// + /// Specular color factor. + /// + /// + /// valid range is 0.0 to 1.0) + /// + [DxfCodeValue(45)] + public double SpecularColorFactor + { + get { return this._specularColorFactor; } + set + { + ObjectExtensions.InRange(value, 0, 1); + this._specularColorFactor = value; + } + } + + private double _specularColorFactor = 1.0; + + /// + /// Specular color. + /// + [DxfCodeValue(92)] + public Color SpecularColor { get; set; } + + /// + /// Specular map blend factor. + /// + /// + /// default = 1.0 + /// + [DxfCodeValue(46)] + public double SpecularMapBlendFactor { get; set; } = 1.0; + + /// + /// Specular map source. + /// + [DxfCodeValue(77)] + public MapSource SpecularMapSource { get; set; } = MapSource.UseImageFile; + + /// + /// Specular map file name. + /// + /// + /// null file name specifies no map. + /// + [DxfCodeValue(4)] + public string SpecularMapFileName { get; set; } + + /// + /// Projection method of specular map mapper. + /// + [DxfCodeValue(78)] + public ProjectionMethod SpecularProjectionMethod { get; set; } = ProjectionMethod.Planar; + + /// + /// Tiling method of specular map mapper. + /// + [DxfCodeValue(79)] + public TilingMethod SpecularMapper { get; set; } = TilingMethod.Tile; + + /// + /// Auto transform method of specular map mapper. + /// + [DxfCodeValue(170)] + public AutoTransformMethodFlags SpecularAutoTransform { get; set; } = AutoTransformMethodFlags.NoAutoTransform; + + /// + /// Transform matrix of specular map mapper. + /// + [DxfCodeValue(47)] + public Matrix4 SpecularMatrix { get; set; } = Matrix4.Identity; + + /// + /// Blend factor of reflection map. + /// + [DxfCodeValue(48)] + public double ReflectionMapBlendFactor { get; set; } = 1.0; + + /// + /// Reflection map source. + /// + [DxfCodeValue(171)] + public MapSource ReflectionMapSource { get; set; } = MapSource.UseImageFile; + + /// + /// Reflection map file name. + /// + /// + /// null file name specifies no map. + /// + [DxfCodeValue(6)] + public string ReflectionMapFileName { get; set; } + + /// + /// Projection method of specular map mapper. + /// + [DxfCodeValue(172)] + public ProjectionMethod ReflectionProjectionMethod { get; set; } = ProjectionMethod.Planar; + + /// + /// Tiling method of reflection map mapper. + /// + [DxfCodeValue(173)] + public TilingMethod ReflectionMapper { get; set; } = TilingMethod.Tile; + + /// + /// Auto transform method of reflection map mapper. + /// + [DxfCodeValue(174)] + public AutoTransformMethodFlags ReflectionAutoTransform { get; set; } = AutoTransformMethodFlags.NoAutoTransform; + + /// + /// Transform matrix of reflection map mapper. + /// + [DxfCodeValue(49)] + public Matrix4 ReflectionMatrix { get; set; } = Matrix4.Identity; + + /// + /// Opacity percent. + /// + [DxfCodeValue(140)] + public double Opacity { get; set; } = 1.0; + + /// + /// Opacity map blend factor. + /// + /// + /// default = 1.0 + /// + [DxfCodeValue(141)] + public double OpacityMapBlendFactor { get; set; } = 1.0; + + /// + /// Opacity map source. + /// + [DxfCodeValue(175)] + public MapSource OpacityMapSource { get; set; } = MapSource.UseImageFile; + + /// + /// Opacity map file name. + /// + /// + /// null file name specifies no map. + /// + [DxfCodeValue(7)] + public string OpacityMapFileName { get; set; } + + /// + /// Opacity method of specular map mapper. + /// + [DxfCodeValue(176)] + public ProjectionMethod OpacityProjectionMethod { get; set; } = ProjectionMethod.Planar; + + /// + /// Tiling method of opacity map mapper. + /// + [DxfCodeValue(177)] + public TilingMethod OpacityMapper { get; set; } = TilingMethod.Tile; + + /// + /// Auto transform method of opacity map mapper. + /// + [DxfCodeValue(178)] + public AutoTransformMethodFlags OpacityAutoTransform { get; set; } = AutoTransformMethodFlags.NoAutoTransform; + + /// + /// Transform matrix of opacity map mapper. + /// + [DxfCodeValue(142)] + public Matrix4 OpacityMatrix { get; set; } = Matrix4.Identity; + + /// + /// Bump map blend factor. + /// + /// + /// default = 1.0 + /// + [DxfCodeValue(143)] + public double BumpMapBlendFactor { get; set; } = 1.0; + + /// + /// Bump map source. + /// + [DxfCodeValue(179)] + public MapSource BumpMapSource { get; set; } = MapSource.UseImageFile; + + /// + /// Bump map file name. + /// + /// + /// null file name specifies no map. + /// + [DxfCodeValue(8)] + public string BumpMapFileName { get; set; } + + /// + /// Bump method of specular map mapper. + /// + [DxfCodeValue(270)] + public ProjectionMethod BumpProjectionMethod { get; set; } = ProjectionMethod.Planar; + + /// + /// Tiling method of bump map mapper. + /// + [DxfCodeValue(271)] + public TilingMethod BumpMapper { get; set; } = TilingMethod.Tile; + + /// + /// Auto transform method of bump map mapper. + /// + [DxfCodeValue(272)] + public AutoTransformMethodFlags BumpAutoTransform { get; set; } = AutoTransformMethodFlags.NoAutoTransform; + + /// + /// Transform matrix of bump map mapper. + /// + [DxfCodeValue(144)] + public Matrix4 BumpMatrix { get; set; } = Matrix4.Identity; + + /// + /// Refraction index. + /// + [DxfCodeValue(145)] + public double RefractionIndex { get; set; } = 1.0; + + /// + /// Bump map refraction factor. + /// + /// + /// default = 1.0 + /// + [DxfCodeValue(146)] + public double RefractionMapBlendFactor { get; set; } = 1.0; + + /// + /// Refraction map source. + /// + [DxfCodeValue(273)] + public MapSource RefractionMapSource { get; set; } = MapSource.UseImageFile; + + /// + /// Refraction map file name. + /// + /// + /// null file name specifies no map. + /// + [DxfCodeValue(9)] + public string RefractionMapFileName { get; set; } + + /// + /// Projection method of refraction map mapper. + /// + [DxfCodeValue(274)] + public ProjectionMethod RefractionProjectionMethod { get; set; } = ProjectionMethod.Planar; + + /// + /// Tiling method of refraction map mapper. + /// + [DxfCodeValue(275)] + public TilingMethod RefractionMapper { get; set; } = TilingMethod.Tile; + + /// + /// Auto transform method of refraction map mapper. + /// + [DxfCodeValue(276)] + public AutoTransformMethodFlags RefractionAutoTransform { get; set; } = AutoTransformMethodFlags.NoAutoTransform; + + /// + /// Transform matrix of refraction map mapper. + /// + [DxfCodeValue(147)] + public Matrix4 RefractionMatrix { get; set; } = Matrix4.Identity; //460 - //Color Bleed Scale + //461 Indirect Dump Scale //462 Reflectance Scale //463 @@ -430,6 +535,11 @@ public class Material : NonGraphicalObject //90 Self-Illuminaton //468 Reflectivity //93 Illumination Model - //94 Channel Flags + + /// + /// Channel Flags. + /// + [DxfCodeValue(94)] + public int ChannelFlags { get; set; } } }