Texture mapping on a surface using c#

Texture mapping on a surface using c#

mayur_kapadnis
Participant Participant
355 Views
3 Replies
Message 1 of 4

Texture mapping on a surface using c#

mayur_kapadnis
Participant
Participant

I have a surface in autocad, i have an image, i want to map that image on that surface, like we do it using RMAT command, how can I do it using c#?

0 Likes
356 Views
3 Replies
Replies (3)
Message 2 of 4

M_Kocyigit
Enthusiast
Enthusiast

Hi @mayur_kapadnis ,

try it with the following code

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(TextureMappingExample))]

public class TextureMappingExample
{
    [CommandMethod("MapTexture")]
    public void MapTexture()
    {
        // Get the active document and database
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;

        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            // Select a surface
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a surface: ");
            peo.SetRejectMessage("\nOnly surfaces are allowed.");
            peo.AddAllowedClass(typeof(Surface), true);

            PromptEntityResult per = doc.Editor.GetEntity(peo);

            if (per.Status != PromptStatus.OK)
                return;

            // Open the surface for write
            ObjectId surfaceId = per.ObjectId;
            Surface surface = tr.GetObject(surfaceId, OpenMode.ForWrite) as Surface;

            if (surface == null)
            {
                doc.Editor.WriteMessage("\nSelected entity is not a surface.");
                return;
            }

            // Add a material with texture
            ObjectId materialId = AddMaterialWithTexture(db, tr, "MyTextureMaterial", "path_to_your_image.jpg");

            // Apply the material to the surface
            if (!materialId.IsNull)
            {
                surface.MaterialId = materialId;
                surface.MaterialMapper.MaterialTransform = Matrix3d.Identity;
            }

            tr.Commit();
        }
    }

    private ObjectId AddMaterialWithTexture(Database db, Transaction tr, string materialName, string imagePath)
    {
        // Get the material dictionary
        ObjectId materialDictId = db.MaterialDictionaryId;
        DBDictionary materialDict = tr.GetObject(materialDictId, OpenMode.ForWrite) as DBDictionary;

        // Check if material already exists
        if (materialDict.Contains(materialName))
        {
            return materialDict.GetAt(materialName);
        }

        // Create a new material
        Material material = new Material
        {
            Name = materialName,
            DiffuseMap = new MaterialMap
            {
                Source = FileMaterialSource.File,
                Filename = imagePath
            }
        };

        // Add the material to the dictionary
        ObjectId materialId = materialDict.SetAt(materialName, material);
        tr.AddNewlyCreatedDBObject(material, true);

        return materialId;
    }
}

 


Furthermore, there is an extensive code from Pavliukas, which was made available in 2007. Many thanks for that.
https://forum.dwg.ru/showthread.php?t=15000&highlight=%EC%E0%F2%E5%F0%E8%E0%EB&pp=10000 

0 Likes
Message 3 of 4

mayur_kapadnis
Participant
Participant

getting these errors:

 

  1. 'Mapper' does not contain a definition for 'MaterialTransform' and no accessible extension method 'MaterialTransform'
    CS1061:
  2. accepting a first argument of type 'Mapper' could be found (are you missing a using directive or an assembly reference?)
    'Material' does not contain a definition for 'DiffuseMap'
  3. Source MaterialMap.Source { get; }
    Gets the data source of the material map; one of the MaterialMap.Source> enumerators.
    CS0200: Property or indexer 'MaterialMap.Source• cannot be assigned to it is read only
  4. CSOI 03: The name 'FileMaterialSource' does not exist in the current context
  5. : 'MaterialMap' does not contain a definition for 'Filename'
0 Likes
Message 4 of 4

M_Kocyigit
Enthusiast
Enthusiast
Have you already tried testing and experimenting with the provided resources? If I have time, I will take a closer look.
0 Likes