.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create Material with VB.NET

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
laurie.comerford
1803 Views, 7 Replies

Create Material with VB.NET

Hi,

 

I'm having a complete blockout on my attempts to create a new material with VB.NET.

 

I've searched the web, I've searched here and found one previous query which was not answered in any way which helped:

Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim tm As DBTransMan = db.TransactionManager
Dim myT As Transaction
' myT = tm.StartTransaction()
Dim tt As Autodesk.AutoCAD.DatabaseServices.Material = New Autodesk.AutoCAD.DatabaseServices.Material


tt.Name = "Mat_new"
tt.Description = "New Material"
myT.AddNewlyCreatedDBObject(tt, True)
myT.Commit()

 It was noted that this code didn't work (which I can confirm) and the suggestion was:

 

"Just a guess, but you probably have to add it to the Database in order to
use it."

 

I can't find any way to add this to the database.

 

Can any one provide an answer?

 

I'm working with the R2010 drawing format it that is relevant.

 

 

Regards,

 

Laurie Comerford

Regards

Laurie Comerford
7 REPLIES 7
Message 2 of 8
Jeff_M
in reply to: laurie.comerford

Hi Laurie,

 

The Materials are stored in a Dictionary. So you need to add the Material to the MaterialsDictionary. Said Dictionary can be obtained from it's ObjectId:

 

Doc.Database.MaterialDictionaryId

 

Does that help?

Jeff_M, also a frequent Swamper
EESignature
Message 3 of 8
Jeff_M
in reply to: laurie.comerford

And this post:

http://forums.autodesk.com/t5/NET/apply-a-texture-from-a-jpg-file-to-the-new-material/m-p/2111719#M8...

has some code (albeit in C#) that goes through the process, although I have not tested it myself.

Jeff_M, also a frequent Swamper
EESignature
Message 4 of 8

I see you already have been helped but since I spent the time writing this I might as well post it.

 

This code is basiclly from here, but shorter. I just left some things out to make it quicker and used VB instead of C#

http://www.theswamp.org/index.php?topic=31452.0

 

The image was first onr that popped up using google with material search

 

        <CommandMethod("CreateMaterial")> _
        Sub CreateMaterial()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor

            Using trx As Transaction = db.TransactionManager.StartTransaction

                Dim matName As String = "NewMaterial"

                Dim mappr As New Mapper(Projection.Sphere, Tiling.Tile, Tiling.Tile,
                                      AutoTransform.TransformObject, Matrix3d.Identity)

                Dim ift As New ImageFileTexture()
                ift.SourceFileName = "C:\Test\Material.png"

                Dim entColor As EntityColor = New EntityColor(ColorMethod.ByAci, 3)
                Dim matColor As New MaterialColor(Method.Override, 1, entColor)

                Dim matMap As New MaterialMap(Source.File, ift, 1, mappr)


                Dim matDiff As New MaterialDiffuseComponent(matColor, matMap)
                Dim mrc As New MaterialRefractionComponent(1.5, matMap)

                Dim mat As New Material
                mat.Name = matName
                mat.Diffuse = matDiff
                mat.Refraction = mrc

                Dim matDict As DBDictionary = trx.GetObject(db.MaterialDictionaryId, OpenMode.ForWrite)
                matDict.SetAt(matName, mat)
                trx.AddNewlyCreatedDBObject(mat, True)

                trx.Commit()

            End Using
        End Sub

 

 

Capture.PNG

 

You can also find your answers @ TheSwamp
Message 5 of 8

Hi Jeffrey,

 

Thank you.  No wonder I couldn't find a way to add the material to the database.

 

"SETAT" !!!

 

Why on earth couldn't they have used "ADD"?

 

I've been hunting through all the options of myDict as defined by:

Dim myDict As DatabaseServices.DBDictionary = myDwg.Database.MaterialDictionaryId.GetObject(OpenMode.ForWrite)

 looking for an Add type of method, but "SETAT" simply didn't ring a bell.

 

A search of the Object model reveals "setat" is only used three times, the other two being the Property Inspector and the Security String.  Why does all this have to be so obscure?

 

 

Regards,

 

Laurie Comerford

 

 

Regards

Laurie Comerford
Message 6 of 8

Jeff_M already had a link and mentioned about the dictionary that answered your question, but for your info use GetAt to grab a dictionary.

You can also find your answers @ TheSwamp
Message 7 of 8
Hallex
in reply to: laurie.comerford

Hi Laurie,

Test this code from post #1 from here

 

http://forum.dwg.ru/showthread.php?t=15000&highlight=%EC%E0%F2%E5%F0%E8%E0%EB&pp=10000

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 8
luisibad
in reply to: Hallex

Hi, is there any option to set the using type, because the type by default is a generic type and I would like to change it to another options.

Thanks.

Luis

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost