Interaction with Files in a Project

In this example, you will look at how you can interact and query specific files that are associated with components in a project. In this example, you wish to know how and where a specific geometry file has been used within any Ansys Workbench project that you have within your directory. After you extract the ScriptingGuideExamples.zip file, you will find the files for this example in the Project_File_Search directory.

To automate this search, you will write a script that performs the following operations. Two different methods that accomplish the same task are demonstrated to illustrate different approaches to project and file navigation.

  1. Starting from a given location, recursively search through all subdirectories to find any Ansys Workbench projects.

  2. For each project, looks for a specified geometry file in using one of two methods:

    1. Get a flat list of all files in the project, and look for the desired file name.

    2. Walk through all systems and their components, and query each component to get the list of files registered to it. Then look if the component is using a file of the specified name. This method is more involved but provides more detailed information (such as the specific system or component using the file). It also gives access to the Ansys Workbench file references to provide detailed information like file size and last modification time.

Workbench Script

The script for this example follows. Each line is numbered for reference in the discussion that follows.

1 	# import the 'os' module, which provides a portable way of using operating system dependent functionality
2 	import os
3 
4 	# A helper function to find all Workbench projects within a specified directory and add them to a list.
5 	# This recursivly calls itself to process subdirectories.
6 	def findProjectFiles(searchDir, fileList):
7 	    print "Searching in %s" % searchDir
8 	    for dirEntry in os.listdir(searchDir):
9 	        fullName = os.path.join(searchDir,dirEntry)
10	        if dirEntry.endswith(".wbpj"):
11	            # Store the full path to the project file
12	            projList.append(fullName)
13	        if os.path.isdir(fullName):
14	            findProjectFiles(fullName, fileList)
15
16
17	# Define starting directory to find project files.  
18	# Here we'll look everywhere within the user's project path root directory.
19	searchDir = AbsUserPathName("ScriptingGuideExamples")
20
21	# Define file name of interest
22	targetFile = "bloodMix1.agdb"
23
24	# Recursively find all WB2 projects within the search directory
25	projList = []
26	findProjectFiles(searchDir, projList)
27
28	# Open a log file to record script progress
29	logFile = open(AbsUserPathName("ScriptingGuideExamples/Project_File_Search/FindFileInProjects.log"),"w")
30
31	for projFile in projList:
32	    try:
33	        Open(FilePath=projFile)
34	    except Exception as ex:
35	        logFile.write("Error opening %s, %s\n" % (projFile, ex))
36	        continue
37	        
38	    # Method 1: Search the list of all project files.  
39	    # This method is simpler, but not as much file information is readily available
40	    for fileName in GetAllFiles():
41	        if fileName.find(targetFile)> -1:
42			logFile.write("--\n")
43		        fileStr = "File %s found in project %s\n"
44		        logFile.write(fileStr % (fileName, projFile))
45		        logFile.flush()
46			
47	    # Method 2: Walk through the systems and components, to find any that are using the file.
48	    # This method is more complex, but provides detailed information through systems, 
49	    # components and file references.  It's also a useful example of general 
50	    # System & Component navigation.
51	    
52	    # Loop over all systems in the project
53	    for system in GetAllSystems():
54	        # Loop over the all components in the system
55	        for component in system.Components:
56	            container = component.DataContainer
57	            # Loop over all file data references associated with the container for the component
58	            for compFile in container.GetFiles():
59	                if compFile.FileName.find(targetFile) > -1:
60			    logFile.write("--\n")
61	                    sysStr = "Target file found in component %s of system named %s in %s. Details:\n"
62	                    fileStr = "   %s, Size %s bytes, Modified %s\n"
63	                    logFile.write(sysStr % (component.DisplayText, system.DisplayText, projFile))
64	                    logFile.write(fileStr % (compFile.Location, compFile.Size, compFile.LastModifiedTime))
65	                    logFile.flush()
66			    
67	logFile.close()
68

Log File

The log file generated by this script should look like this:

--
Target file found in component Geometry of system named Static Structural in 
     C:\Users\neUser\DemoProjects\pipe1.wbpj. Details:
   E:\data\Models\pipe_demo\pipe.x_t, Size 6934 bytes, Modified 06/07/2009 11:50:53 AM
--
File E:\data\Models\pipe_demo\pipe.x_t found in project C:\Users\neUser\DemoProjects\pipe1.wbpj
--
Target file found in component Geometry of system named Static Structural in 
     C:\Users\neUser\Working\pipeDemo.wbpj. Details:
   E:\data\Models\pipe_demo\pipe.x_t, Size 6934 bytes, Modified 06/07/2009 11:50:53 AM
--
File E:\data\Models\pipe_demo\pipe.x_t found in project C:\Users\neUser\Working\pipeDemo.wbpj

Discussion

This example demonstrates how to navigate through systems and components in a project, as well as useful queries and data entities for working with files. The following discussion refers to the specified line numbers of the example script to illustrate some of these concepts and constructs. Discussion points from earlier examples will not be repeated here.

Lines 4-14

This function finds all project files within a specified directory. The Python os.listdir function returns all file and directory names in the specified location. If the name ends with .wbpj, Ansys Workbench stores the full path to a list of projects. If the name is a directory, Ansys Workbench recursively calls the function to search the subdirectory.

Lines 32-36

Here, the script demonstrates exception handling to catch any errors that result from opening the project file and report the error to the log file. The continue statement skips to the next entry in the project file loop.

Lines 38-45

This method uses the GetAllFiles() query to get a flat list of all files used in the project and looks at each entry to see if contains the target file name. If so, the script records the full path to the target file and the project file to the log.

Line 53

The GetAllSystems() query is used to loop over all systems in the project.

Line 55

The Components property in the system is accessed to loop over the set of components in the system.

Line 56

The DataContainer property is used to access the data within the component.

Line 58

The GetFiles() method on a container is used to return a list of file references that have been associated to that container.

Lines 59-65

The script looks at the FileName property of each file reference to see if it contains the target file name. If so, other properties of the file reference, system, and component are used to record information about the file and its location within the project to the log file.