Materials

Importing a Material Library to a Text File


This macro shows you how to create or complete a material library from a text format.

 

importMaterialLibrary is launched in CATIA. An existing material library document (with ".CATMaterial" extension) must be found in the CATDocView.

A text file containing all informations for materials should exist on disk. Its extension should be ".matlib".

importMaterialLibrary.CATScript is located in the CAAScdMatUseCases module. Execute macro (Windows only).

After the import, the filled material library will be displayed in CATIA, with all families and materials. Note that material thumbnails are default thumbnails (a mat grey sphere) and do not reflect the material's parameters values. To update its thumbnail, a material should be edited in CATIA.

importMaterialLibrary includes four steps:

  1. Prolog
  2. Retrieve the text file and open it
  3. Create and set all entities (families, materials, properties) in current .CATMaterial file
  4. Save the modified material library file

Prolog


            ' Get the file system object
	        Dim oFileSys as FileSystem
	        Set oFileSys = CATIA.FileSystem

	     ' Get the documents collection
	        Dim oCollection As Documents
	        Set oCollection = CATIA.Documents

	     ' test if no document is open
	        If 0=oCollection.Count Then 
		        msgbox "A new material library document must be active to execute this macro.", vbOKOnly, "Import Material Library"
		        Exit Sub
	        End If

	     ' Get material library
	        Dim oCat As Document
	        Set oCat = CATIA.ActiveDocument

	     ' test if the active document is a material library (CATMaterial)
	        If 0=InStr(oCat.Name, ".CATMaterial") Then
		        msgbox "A new material library document must be active to execute this macro.", vbOKOnly, "Import Material Library"
		        Exit Sub
	        End If
    	       ...
            

At first, this macro tests if a document is active in CATIA and if this document is a CATMaterial one.

[Top]

Retrieve the text file and open it

	       ...
             ' Get name of the material library text file
	        Dim sAnswer As String
	        sAnswer = CATIA.FileSelectionBox("Select a material library text file", "*.matlib", CatFileSelectionModeOpen)

	        If ""<>sAnswer Then ' CANCEL
		
            ' Determine the file separator
	        Dim sSep As String
	        sSep = oFileSys.FileSeparator

	    ' Create the material library text file
	        Dim oFileIn As File    
	        Set oFileIn = oFileSys.GetFile(sAnswer)
	        Dim oStream As TextStream
	        Set oStream = oFileIn.OpenAsTextStream("ForReading")
         
                ... 
            

The macro file opens a file selector to be able to load the material library text file.

This text file should be formated like this:

##############################################################################
# #
# MATERIAL LIBRARY TEXT FILE #
# #
##############################################################################


LIBRARY=MyCatalog
                 FAMILY=Construction
                                MATERIAL=Concrete
                                                   PROPERTY=Rendering
                                                                  MappingType=3
                                                                  AdaptiveCoeff=0
                                                                  PreviewSize=50
                                                                  AmbientCoefficient=0.80
                                                                  DiffuseCoefficient=1
                                                                  SpecularCoefficient=0
                                                                  SpecularExponent=0
                                                                  TransparencyCoefficient=0
                                                    .....

 

[Top]

Create and set all entities (families, materials, properties) in current .CATMaterial file

	...
              ' Read the input file line by line
		Do Until oStream.AtEndOfStream

	     ' Read the current line
		sLine = oStream.ReadLine
		sBuffer = Left(sLine, 1)

	     ' Test if the line is empty or a comment
		If 0<>StrComp(sBuffer, "#") And 0<Len(sBuffer) And 0<>FindChar(sLine) Then
				
	     ' Parse the line, determine param and value
		If 0=ParseLine(sLine, sParam, sValue) Then ' test for syntax error
		  SYNTAX_ERROR iInc
		  Exit Sub
		Else
             ' Special case for the first param (LIBRARY)
		  If iCurLevel=0 And 0<>StrComp(sParam, "LIBRARY") Then
		    SEMANTIC_ERROR iCurLevel
		    Exit Sub
		  Else
		    If 0<>iCurLevel Then
		      If LevelParam(sParam) > iCurLevel Then ' test for semantic error
		  	SEMANTIC_ERROR iCurLevel
			Exit Sub
		      Else
			If 0<>StrComp(sParam, "FAMILY") Then
			  bMinimumDone = True
			End If
    		' Case with no error
			If 0=StrComp(sParam, "FAMILY") Then ' FAMILY treatment
		' Init families
			  Set oFamilies = oCat.Families
		' Init the new family
			  Set oFamily = oFamilies.Add
		' Affect name
			  oFamily.Name = sValue
			Else
			  If 0=StrComp(sParam, "MATERIAL") Then ' MATERIAL treatment
		' Init materials
			    Set oMaterials = oFamily.Materials
		' Init the new material
			    Set oMaterial = oMaterials.Add
		' Affect name
			    oMaterial.Name = sValue
		' Affect icon
			    If oFileSys.FileExists(sFolderIcon & sSep & oFamily.Name & sSep & oMaterial.Name & ".jpg") Then
					oMaterial.PutIcon(sFolderIcon & sSep & oFamily.Name & sSep) ' & oMaterial.Name & ".jpg")
		            End If
			  Else
			    If 0=StrComp(sParam, "PROPERTY") Then ' PROPERTY treatment

    			      If 0=StrComp(sValue, "Rendering") Then ' rendering case
		' Add rendering data on the new material
				Set oRenderingMaterial = oMaterial.CreateRenderingData
			      End If
			      If 0=StrComp(sValue, "AnalysisIsotropic") Then ' AnalysisIsotropic case
		' Add Analysis data on the new material
				Set oAnalysisMaterial = oMaterial.CreateAnalysisData("SAMIsotropicMaterial")
			      End If
			      If 0=StrComp(sValue, "AnalysisOrthotropic") Then ' AnalysisOrthotropic case
		' Add Analysis data on the new material
				Set oAnalysisMaterial = oMaterial.CreateAnalysisData("SAMOrthotropicMaterial")
			      End If
			  Else ' AmbientCoefficient, DiffuseCoefficient, ... treatment

		' One variable mode (format: var)
			      If 0=StrComp(sParam, "MAPPINGTYPE") Then
				oRenderingMaterial.MappingType = sValue
			      End If
			      If oRenderingMaterial.MappingType = 5 Then ' Manual mapping
				If 0=StrComp(sParam, "ADAPTIVECOEFF") Then
				  oRenderingMaterial.AdaptiveCoeff = sValue
				End If
			      End If
			      If 0=StrComp(sParam, "PREVIEWSIZE") Then
				oRenderingMaterial.PreviewSize = sValue
			      End If
			      If 0=StrComp(sParam, "AMBIENTCOEFFICIENT") Then
				oRenderingMaterial.AmbientCoefficient = sValue
			      End If
			...

The macro loops on each line of the text file :

  • if "FAMILY" keyword is found, it creates a new family in material library
  • if "MATERIAL" keyword is found, it creates a new material in last created family, with the provided name
  • if "PROPERTY" keyword is found, it adds new applicative data to tha last created material
  • if a parameter name keyword is found (like "MAPPINGTYPE" or "AMBIENTCOEFFICIENT"), parameter of last created material is modified

[Top]

Save the modified material library file

	...
         	' Save the new CATMaterial
		    oCat.SaveAs(Left(sAnswer, InStr(sAnswer, ".matlib")-1) & ".CATMaterial")
	        ' End message
	            msgbox "Operation succeed." & Chr(10) & "The material library has been saved at this location :" & Chr(10) & Chr(10) & Left(sAnswer, InStr(sAnswer, ".matlib")-1) & ".CATMaterial", vbOKOnly, "Import Material Library"
	            msgbox "Operation succeed." & Chr(10) & "The material library has been imported.", vbOKOnly, "Import Material Library"
            


In Short

This use case presents a macro which can be useful to create a material library from an external text file.

The text format file to use is very simple and allows therefore the material library to be easily customized.

[Top]


Copyright © 1994-2003, Dassault Systèmes. All rights reserved.