Setting U and V tile scale of a newly created material using .NET

By Adam Nagy

I’m creating a new material using the .NET API and I can set most of the things I want, but I cannot find where to set the U and V tile scales for the diffuse I’m adding.

Solution

You need to set those using the transformation matrix. The following sample shows how:

using System;
using Autodesk.AutoCAD.DatabaseServices;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
 
namespace MaterialTest
{
  public class Commands
  {
    public static void AddMaterialToLibrary(
      String sMaterialName, String sTextureMapPath)
    {
      Document doc = acApp.DocumentManager.MdiActiveDocument;
      using (
        Transaction acTrans = 
          doc.TransactionManager.StartTransaction())
      {
        // Get the material library
        DBDictionary matLib = 
          (DBDictionary)acTrans.GetObject(
            doc.Database.MaterialDictionaryId, OpenMode.ForRead);
 
        // If this material does not exist
        if (matLib.Contains(sMaterialName) == false)
        {
          // Create the texture map image
          ImageFileTexture tex = new ImageFileTexture();
          tex.SourceFileName = sTextureMapPath;
 
          // Create the material map
          double uScale = 15, vScale = 20;
          double uOffset = 25, vOffset = 30;
          Matrix3d mx = new Matrix3d(new double[]{
              uScale, 0, 0, uScale * uOffset, 
              0, vScale, 0, vScale * vOffset, 
              0, 0, 1, 0, 
              0, 0, 0, 1});
 
          Mapper mapper = 
            new Mapper(
              Projection.Cylinder, Tiling.Tile, Tiling.Tile, 
              AutoTransform.None, mx);
 
          MaterialMap map = 
            new MaterialMap(Source.File, tex, 1.0, mapper);
 
          // Set the opacity and refraction maps
          MaterialDiffuseComponent mdc = 
            new MaterialDiffuseComponent(new MaterialColor(), map);
          MaterialRefractionComponent mrc = 
            new MaterialRefractionComponent(2.0, map);
 
          // Create a new material
          Material mat = new Material();
          mat.Name = sMaterialName;
          mat.Diffuse = mdc;
          mat.Refraction = mrc;
          mat.Mode = Mode.Realistic;
          mat.Reflectivity = 1.0;
          mat.IlluminationModel = IlluminationModel.BlinnShader;
 
          // Add it to the library
          matLib.UpgradeOpen();
          matLib.SetAt(sMaterialName, mat);
          acTrans.AddNewlyCreatedDBObject(mat, true);
          acTrans.Commit();
        }
      }
    }
 
    [CommandMethod("Test")]
    public static void Test()
    {
      AddMaterialToLibrary(
        "MyMaterial", @"C:MaterialTestMaterialTesttest.png");
    }
  }
}

Here you see the properties of the material we created in the user interface:
Mymaterial


Comments

3 responses to “Setting U and V tile scale of a newly created material using .NET”

  1. Thanks for this site.
    Just one little thing … could you change the gold lettering to a colour with better contrast on a white/grey background?

  2. vilas jadhav Avatar
    vilas jadhav

    Dear Sir,
    It is very useful task for me . but I am in strong in Auto lisp , It is possible to create new material library in auto lisp.
    thanks
    Vilas Jadhav

  3. Can i set uv to rotate?

Leave a Reply to ChenXCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading