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
Solved! Go to Solution.
Solved by laurie.comerford. Go to Solution.
And this post:
has some code (albeit in C#) that goes through the process, although I have not tested it myself.
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
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
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
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