Skip to main content
10-Marble
March 2, 2021
Solved

CREO JLINK - List Sub Dependencies

  • March 2, 2021
  • 1 reply
  • 2682 views

Hello,


Thanks for helping me out earlier, I was hoping if someone can help on this. What I need is to list down all dependencies of a Drawing, currently I am using model.ListDependencies(), but it only lists top-level.
For example, 

Drawing 

____Assembly

____________Model 1

____________Model 2

 

Currently I only get Assembly, not Model 1 and Model 2. I just need the filenames of sub-models.

 

Thanks

 

 

Best answer by sjuraj

you need to recursively list dependencies for every dependency get by listDependencies

this example is similar to what you need

it list all files in folders and subfolders (in your case it will be dependencies and subdependencies)

 

import java.io.File;

public class Filewalker {

 public void walk( String path ) {

 File root = new File( path );
 File[] list = root.listFiles();

 if (list == null) return;

 for ( File f : list ) {
 if ( f.isDirectory() ) {
 walk( f.getAbsolutePath() );
 System.out.println( "Dir:" + f.getAbsoluteFile() );
 }
 else {
 System.out.println( "File:" + f.getAbsoluteFile() );
 }
 }
 }

 public static void main(String[] args) {
 Filewalker fw = new Filewalker();
 fw.walk("c:\\" );
 }

}

 

1 reply

sjuraj15-MoonstoneAnswer
15-Moonstone
March 2, 2021

you need to recursively list dependencies for every dependency get by listDependencies

this example is similar to what you need

it list all files in folders and subfolders (in your case it will be dependencies and subdependencies)

 

import java.io.File;

public class Filewalker {

 public void walk( String path ) {

 File root = new File( path );
 File[] list = root.listFiles();

 if (list == null) return;

 for ( File f : list ) {
 if ( f.isDirectory() ) {
 walk( f.getAbsolutePath() );
 System.out.println( "Dir:" + f.getAbsoluteFile() );
 }
 else {
 System.out.println( "File:" + f.getAbsoluteFile() );
 }
 }
 }

 public static void main(String[] args) {
 Filewalker fw = new Filewalker();
 fw.walk("c:\\" );
 }

}

 

10-Marble
March 2, 2021

Thank you, yes I found no method in API to do this, had to create own method which iterates over each dependency.

Here it is,

Models dependModels = drawingFile.ListModels();
String modelName = "";

for (int i = 0; i < dependModels.getarraysize(); i++) {
      Model singleModel = dependModels.get(i);
      if (singleModel.GetType() == ModelType.MDL_ASSEMBLY) {
              Assembly as = (Assembly) singleModel;
              Dependencies asDependencies = as.ListDependencies();
              for (int j = 0; j < asDependencies.getarraysize(); j++) {
                    modelName = asDependencies.get(j).GetDepModel().GetFileName();
              }
      }

}

15-Moonstone
March 3, 2021

There is no such method, you need to create your own recursive method. This will list first and sub level dependencies: 

 

static void listAllDependencies(Assembly asm, Set<ModelDescriptor> deps) throws jxthrowable {
Dependencies dependencies = asm.ListDependencies();
for(int i = 0; i < dependencies.getarraysize(); i++) {
Dependency dependency = dependencies.get(i);
ModelDescriptor md = dependency.GetDepModel();
ModelType type = md.GetType();
deps.add(md);
if(type.getValue() == ModelType._MDL_ASSEMBLY) {
Assembly subAsm = (Assembly) session.RetrieveModel(md);
listAllDependencies(subAsm, deps);
}
}
}

To try it you can do 

Assembly asm = (Assembly) session.GetCurrentModel();
Set<ModelDescriptor> deps = new HashSet<>();
listAllDependencies(asm, deps);
StringBuilder sb = new StringBuilder();
for(ModelDescriptor md : deps) {
sb.append(md.GetFileName()).append("\n");
}
session.UIShowMessageDialog(sb.toString(), null);