Author Topic: Modify Opacity of a Material?  (Read 3919 times)

0 Members and 1 Guest are viewing this topic.

dugk

  • Guest
Modify Opacity of a Material?
« on: December 28, 2009, 01:05:21 PM »
Can someone give me some leads on how to modify the Opacity of a Material?

I've already found/created code on verifying a Material exists, creating a Material and applying the Material created.

Thanks!
Doug

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Modify Opacity of a Material?
« Reply #1 on: December 28, 2009, 11:34:20 PM »

Hello Doug,

I may be able to find some time to look, but I'd need you to post some starter code.

regards
Kerry
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

dugk

  • Guest
Re: Modify Opacity of a Material?
« Reply #2 on: December 29, 2009, 12:14:52 AM »
Here is what I have for creating the Material and for verifying that it exists or not.

        static string materialName = "Clear";

        static public void createMaterialClear(double mocValue)
        {
            Document acActiveDoc = AcadApp.DocumentManager.MdiActiveDocument;
            Database acCurDb = acActiveDoc.Database;

            Mapper mapper1 = new Mapper(Projection.InheritProjection, Tiling.Tile, Tiling.Tile,
                AutoTransform.TransformObject, Matrix3d.Identity);

            ImageFileTexture mttr = new ImageFileTexture();

            MaterialMap map = new MaterialMap();

            EntityColor eclr = new EntityColor(250, 250, 250);
            MaterialColor mc = new MaterialColor(Method.Override, 1, eclr);
            MaterialDiffuseComponent mdc = new MaterialDiffuseComponent(mc, map);
            MaterialSpecularComponent mck = new MaterialSpecularComponent(mc, map, 1);
            MaterialOpacityComponent moc = new MaterialOpacityComponent(mocValue, map);
            MaterialRefractionComponent mrfr = new MaterialRefractionComponent(1.5, map);

            Material Mat = new Material();

            Mat.Name = materialName;
            Mat.Description = "material";
            Mat.Diffuse = mdc;
            Mat.Specular = mck;
            Mat.Refraction = mrfr;
            Mat.Reflectivity = 1;
            Mat.Reflection = map;
            Mat.Opacity = moc;
            Mat.Ambient = mc;
            Mat.Bump = map;
            Mat.SelfIllumination = 0;
            Mat.Translucence = 0.1;

            ObjectId MatId = ObjectId.Null;
            using (Transaction tr = acActiveDoc.TransactionManager.StartTransaction())
            {
                MatId = ObjectId.Null;
                if (zzExistMaterial(Mat.Name))
                    return;
                else
                {
                    using (DBDictionary dict = (DBDictionary)tr.GetObject
                    (acCurDb.MaterialDictionaryId, OpenMode.ForWrite))
                    {
                        MatId = dict.SetAt(Mat.Name, Mat);
                        tr.AddNewlyCreatedDBObject(Mat, true);
                    }
                    tr.Commit();
                }
            }
        }

        static public bool zzExistMaterial(string matname)
        {
            Document acActiveDoc = AcadApp.DocumentManager.MdiActiveDocument;
            Database acCurDb = acActiveDoc.Database;
            bool doesTheMaterialAlreadyExist = false;
            ObjectId id = ObjectId.Null;
            using (Transaction tr = acActiveDoc.TransactionManager.StartTransaction())
            {
                using (DBDictionary dict = (DBDictionary)tr.GetObject(acCurDb.MaterialDictionaryId, OpenMode.ForRead))
                {
                    if (dict.Contains(matname))
                        id = dict.GetAt(matname);
                    if (id != ObjectId.Null)
                        doesTheMaterialAlreadyExist = true;
                }
                tr.Commit();
            }
            return doesTheMaterialAlreadyExist;
        }

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Modify Opacity of a Material?
« Reply #3 on: December 29, 2009, 03:18:11 AM »
I don't understand enough about Materials apparently

I fed your beast with this :
Code: [Select]

    [CommandMethod("DoIt")]
    public static void DoMaterialStuff()
    {
        double moc = 80.5;
        createMaterialClear(moc);
    }


The code seems to work, that is ; the Material Mat object in the code editor has the Opacity assigned and the Material is created in AutoCAD
... BUT the opacity stays at 100, not at 80.5 .... ??

Sorry I can't do better at the moment  :oops:
Here's my piccys

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Modify Opacity of a Material?
« Reply #4 on: December 29, 2009, 02:32:10 PM »
Kerry, as an aside, what add-ons are you running and is it Visual Studio Pro you're running them in or something else? The vertical lines between braces remind me of something...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Modify Opacity of a Material?
« Reply #5 on: December 29, 2009, 04:57:25 PM »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Modify Opacity of a Material?
« Reply #6 on: December 29, 2009, 05:13:17 PM »
Thought it was....only available for the full biccy though as I understand...new year purchase.

What do you think of Code Rush Express so far?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Modify Opacity of a Material?
« Reply #7 on: December 29, 2009, 05:20:33 PM »
Thought it was....only available for the full biccy though as I understand...new year purchase.

What do you think of Code Rush Express so far?

I quite like it :
It's non-intrusive and has some helpfull features

just wish I was coding more to make better use of it.
... perhaps I need to take control of my life again.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: Modify Opacity of a Material?
« Reply #8 on: December 29, 2009, 05:21:56 PM »
Visually lining up braces is worth the price of admission alone me thinks, let alone what else it does...

dugk

  • Guest
Re: Modify Opacity of a Material?
« Reply #9 on: January 05, 2010, 03:49:36 PM »
FYI - I did finally figure out what I needed, and learned a bit in the process.

Code:
            using (acActiveDoc.LockDocument())
            {
                using (Transaction tr = acActiveDoc.TransactionManager.StartTransaction())
                {
                    if (thruView.ThruView.zzExistMaterial(materialName) == true)
                    {
                        ObjectId psClearID = thruView.ThruView.zzGetMaterialID(materialName);
                        if (psClearID != null)
                        {
                            Material psClearMat = (Material)tr.GetObject(psClearID, OpenMode.ForWrite);
                            MaterialOpacityComponent moc = thruView.ThruView.createPsClearMaterialMap(Convert.ToInt32(lblValue.Text) / 100.0);
                            psClearMat.Opacity = moc;
                            tr.Commit();
                        }
                    }
                }
            }



        static public ObjectId zzGetMaterialID(string matname)
        {
            Document acActiveDoc = AcadApp.DocumentManager.MdiActiveDocument;
            Database acCurDb = acActiveDoc.Database;
            ObjectId id = ObjectId.Null;
            using (Transaction tr = acActiveDoc.TransactionManager.StartTransaction())
            {
                using (DBDictionary dict = (DBDictionary)tr.GetObject(acCurDb.MaterialDictionaryId, OpenMode.ForRead))
                    if (dict.Contains(matname))
                       id = dict.GetAt(matname);
                tr.Commit();
            }
            return id;
        }

        static public bool zzExistMaterial(string matname)
        {
            Document acActiveDoc = AcadApp.DocumentManager.MdiActiveDocument;
            Database acCurDb = acActiveDoc.Database;
            bool doesTheMaterialAlreadyExist = false;
            ObjectId id = ObjectId.Null;
            using (Transaction tr = acActiveDoc.TransactionManager.StartTransaction())
            {
                using (DBDictionary dict = (DBDictionary)tr.GetObject(acCurDb.MaterialDictionaryId, OpenMode.ForRead))
                {
                    if (dict.Contains(matname))
                        id = dict.GetAt(matname);
                    if (id != ObjectId.Null)
                        doesTheMaterialAlreadyExist = true;
                }
                tr.Commit();
            }
            return doesTheMaterialAlreadyExist;
        }

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Modify Opacity of a Material?
« Reply #10 on: January 06, 2010, 08:19:08 AM »

great to see a good result Doug.  :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.