Materials

Exporting a Material Library to a Text File


This macro shows you how to export an existing  material library in a text format and/or an html format.

 

exportMaterialLibrary is launched in CATIA. An existing document called "MyCatalog.CATMaterial" must be found in the CATDocView.

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

 

exportMaterialLibrary includes three steps:

  1. Prolog
  2. Create the folder and the file for output
  3. Declarations
  4. Write material properties into output files

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 material library document must be active to execute this macro.", vbOKOnly, "Export 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 material library document must be active to execute this macro.", vbOKOnly, "Export Material Library"
		Exit Sub
	End If

	' Test if the document has been saved to disc
	If ""=oCat.Path Then
		msgbox "A material library saved to disc must be active to execute this macro.", vbOKOnly, "Export Material Library"
		Exit Sub
	End If

	...

At first, this macro tests if a document is active in CATIA, if this document is a CATMaterial one and if this catalog has already been saved to disc.

[Top]

Create the folder and the file for output

	...
	' Create folder for outputs
	Dim sFolderPath As String
	sFolderPath = oCat.Path & sSep & sName & "_Exported"
	If oFileSys.FolderExists(sFolderPath) Then
		MsgBox "WARNING: Directory " & sFolderPath & Chr(10) & Chr(9) & "  already exists and will be recreated", vbOKOnly, "Export Material Library"
		DeleteFolderRecursive oFileSys, sFolderPath, sSep
		
		On Error Resume Next
		Dim CreatedFolder As Folder
		Set CreatedFolder = oFileSys.CreateFolder(sFolderPath)
		Set CreatedFolder = NOTHING
		Dim erreur As Integer
		erreur = Err.Number
		Err.Clear
		If (erreur <> 0) Then
			MsgBox  "ERROR: Impossible to create folder " & sFolderPath & Chr(10)  & Chr(9) & "Folder may be in use", vbOKOnly, "Export Material Library"
			Err.Raise erreur
			Exit Sub
		End If
	Else
	   	oFileSys.CreateFolder(sFolderPath)
	End If

	' Create the name of the output file
	Dim sFileOutPath As String
	sFileOutPath = sFolderPath & sSep & sName & ".matlib"

	' Create the material library text file
	Dim oFileOut As File    
	Set oFileOut = oFileSys.CreateFile(sFileOutPath, FALSE)
	Dim oStream As TextStream
	Set oStream = oFileOut.OpenAsTextStream("ForWriting")

	' Header of the output file
	oStream.Write "##############################################################################" & Chr(10)
	oStream.Write "#                                                                            #" & Chr(10)
	oStream.Write "#                     MATERIAL LIBRARY TEXT FILE                             #" & Chr(10)
	oStream.Write "#                                                                            #" & Chr(10)
	oStream.Write "##############################################################################" & Chr(10)
	oStream.Write Chr(10) & Chr(10)
	oStream.Write "LIBRARY=" & sName & Chr(10)

	... 

The macro starts to create a new folder "MyCatalog" under the material catalog directory, where all results files are created.

If folder already exists, a warning message is emitted and the macro try to recreate it.

The material catalog is exported under 2 different format:

  • Text format, in a file named "MyCatalog.matlib" 

  • HTML format, in several html files whose header file is named "MyCatalog.html"

[Top]

Declarations

	...
' Declarations
	Dim oFamilies As MaterialFamilies
	Dim oFamily As MaterialFamily
	Dim I As Int, J As Int, K As Int
	Dim oMaterials As Materials
	Dim oMaterial As Material
	Dim oRenderingMaterial As RenderingMaterial
	Dim oAnalysisMaterial As AnalysisMaterial
	Dim oTab(3) As CATSafeArrayVariant
	Dim iNum As Short
	Dim iNumDouble As Double
	Dim sTemp As String

	' Create the html family output file
	Dim oFamilyHtmlFileOut As File    
	Dim oFamilyHtmlStream As TextStream
	Dim oMaterialHtmlFileOut As File    
	Dim oMaterialHtmlStream As TextStream
	...

[Top]

Write material properties into output files

	...
	' Init families
	Set oFamilies = oCat.Families

	' Family loop
	For I = 1 To oFamilies.Count

		' Init family
		Set oFamily = oFamilies.Item(I)
		oStream.Write Chr(9) & "FAMILY=" & oFamily.Name & Chr(10)
		' Create image folder
		oFileSys.CreateFolder(sFolderPath & sSep & oFamily.Name)

		' Init materials
		Set oMaterials = oFamily.Materials

		' Html families table
		If ((I-1) Mod 3) = 0 Then
			oHtmlStream.Write "<TR>" & Chr(10)
		End If
		oHtmlStream.Write "<TD>"
		If oMaterials.Count >0 Then
			oHtmlStream.Write "<A href='" & oFamily.Name & ".html'><IMG src='" & oFamily.Name & sSep & oMaterials.Item(1).Name & ".jpg'></A>"
		End If
		oHtmlStream.Write "</TD>" & Chr(10)
		oHtmlStream.Write "<TD width=200><A href='" & oFamily.Name & ".html' OnMouseOver=''>" & oFamily.Name & "</A></TD>" & Chr(10)
		If ((I Mod 3) = 0) Or (I=oFamilies.Count) Then
			oHtmlStream.Write "</TR>" & Chr(10)
		End If

		' Html family page
		Set oFamilyHtmlFileOut = oFileSys.CreateFile(sFolderPath & sSep & oFamily.Name & ".html", FALSE)
		Set oFamilyHtmlStream = oFamilyHtmlFileOut.OpenAsTextStream("ForWriting")

		' Header of the family html output file
		HtmlHeader oFamilyHtmlStream, "FAMILY '" & oFamily.Name & "' OF THE MATERIAL LIBRARY '" & sName & "'", "This page presents a family of the material library '<A href='" & sName & ".html'>" & sName & "</A>' used for CATIA V5.", oFamily.Name, 20

		' Material loop
		For J = 1 To oMaterials.Count

			' Init material
			Set oMaterial = oMaterials.Item(J)
			oStream.Write Chr(9) & Chr(9) & "MATERIAL=" & oMaterial.Name & Chr(10)
 
			' Save icon
			oMaterial.GetIcon(sFolderPath & sSep & oFamily.Name & sSep) ' & oMaterial.Name & ".jpg")

	...

The format of text file generated looks like:

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


LIBRARY=MyCatalog
                 FAMILY=Construction
                                MATERIAL=Concrete
                                                   PROPERTY=Rendering

                                                                  MappingType=3
                                                                  AdaptiveCoeff=0
                                                                  PreviewSize=50
                                                                  AmbientCoefficient=0.800000011920929
                                                                  DiffuseCoefficient=1
                                                                  SpecularCoefficient=0
                                                                  SpecularExponent=0
                                                                 TransparencyCoefficient=0
                                                    .....

The html format allows to present graphically the several material families, and allows to navigate into these families to display each material and its rendering and analysis properties:

[Top]


In Short

This use case presents a macro which can be useful to send a material library to an external user.

The text format file generated is very simple to allow easy material properties modifications with a common text editor. By modifying the macro, this text format can also be easily customized.

The html format can be more useful to make a graphical presentation of a material catalog content. It also offers a material preview icon for each material.

[Top]


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