cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

CREO JLINK - List Sub Dependencies

hamzawaseem
6-Contributor

CREO JLINK - List Sub Dependencies

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

 

 

Hamza
1 ACCEPTED SOLUTION

Accepted Solutions
sjuraj
13-Aquamarine
(To:hamzawaseem)

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:\\" );
    }

}

 

View solution in original post

3 REPLIES 3
sjuraj
13-Aquamarine
(To:hamzawaseem)

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:\\" );
    }

}

 

hamzawaseem
6-Contributor
(To:sjuraj)

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();
              }
      }

}

Hamza
sjuraj
13-Aquamarine
(To:hamzawaseem)

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);
Top Tags