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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Quick Close and Open a model using JLink

swapnildhengle
8-Gravel

Quick Close and Open a model using JLink

Hello All,

Greetings,

I am trying to write a tool to read one level structure of the Assembly from folders.

There are around 2k assemblies per folder on iterative basis.

I am using RetrieveModelWithOpts API to load a model. This API looks keeps the already opened model in session.

Is there a way to close opened model or clear the model?

How I can make this Assembly structure extraction quick

 

Thanks,

Swapnil

10 REPLIES 10

Hi, you can use Model.Erase().

We using script for Models and drawings export and we using this sequence:

 

activeModel = currentSession.GetModelFromFileName(fullName);

...

activeDrawing = currentSession.GetModelFromFileName(drawingName);

...

activeDrawing.Erase();

 

 

 

Hi,

 

Thanks for your valuable feedback,

 

Here I am trying to open assembly and get the level 1 children, The complete operation I need to perform silently no UI Update.

 

I have tried to use the way you have suggested.

 

The return model is null, don't know if I am missing something.

 

Thanks,

Swapnil

Hello!
Here is the code I use to create a model from a template:

 

 

try	{
    		
    		Session curSession = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
			String modelLocation = "your_location";
			String model_name = "your_prt_name.prt";
			ModelDescriptor desc = null;
			curSession.GetCurrentWindow().Activate();
			desc = pfcModel.ModelDescriptor_CreateFromFileName(model_name);
			desc.SetPath (modelLocation);
			Model template = curSession.RetrieveModelWithOpts(desc, pfcSession.RetrieveModelOptions_Create());
		    Model newPart = template.CopyAndRetrieve("Test_part", null);
		    Window newWindow = curSession.CreateModelWindow(newPart); 
		    newPart.Display(); 
		    curSession.SetCurrentWindow(newWindow); 
		    newWindow.Activate();
		    template.Erase();
		
		} catch (Exception e) {
			
			Session session = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
			Model template = session.GetModel("your_prt_name.prt", ModelType.MDL_PART);
			template.Erase();
	    
		}

 

 

Best Regards!

JDK
7-Bedrock
7-Bedrock
(To:VladiSlav)

Hi, VladiSlav,can you share the file with all the code?

VladiSlav
17-Peridot
(To:JDK)

Hi!

here is my file.

JDK
7-Bedrock
7-Bedrock
(To:VladiSlav)

Thanks,  VladiSlav ,How long have you been doing this?

VladiSlav
17-Peridot
(To:JDK)

not very long)

JDK
7-Bedrock
7-Bedrock
(To:VladiSlav)

do you have any more code ready?

VladiSlav
17-Peridot
(To:JDK)

Parts of my code are on this site.

Hi... CREOSON (OpenSource) requires JLINK and is a bit easier to use than rolling Java code from scratch... (a lot of helper and wildcard function options built in to CREOSON to make things easier).

 

What you are looking to do is actually pretty simple... and you can use your language of choice to code this basic framework 'psudo code' shown here:

 

let asyModels = creo : list_files (filename = *.asm)

for each asyName in asyModels

  • file : erase      // clear creo session
  • file : open (file = asyName)  // open the asyName
  • asyComps = bom : get_paths (file = asyName, top_level = true)
  • --> report the top level bom content - or write to a file....
  • continue for loop until completed

I hope all that makes sense - you can use CREOSON with any programming or scripting language - and there is a pre-build python library (creopyson) available.  We are working on JavaScript web-client/Node.js library to make it even easier to focus on the workflow rather than the "code" ... here is an example : (currently beta code internally - but the process works if you roll-your-own code)

 

function getBomData(fileName) {

        let fObj = new creo.FileObj();
        let bObj = new creo.BomObj();

        return new Promise(function (resolve, reject) {

            fObj.erase()
                .then(() => {

                    fObj.file = fileName;
                    return fObj.open();
                    
                })
                .then(() => {

                    bObj.file = fileName;
                    bObj.top_level = true;
                    return bObj.get_paths();
                    
                })
                .then((bomData) => {
                    
                    console.log('--- TOP-LEVEL BOM DATA for : '+fileName);

                    bomData.children.children.forEach((childModel) => {
                        console.log(' --> '+childModel.file);
                    });
                    
                })
                .then(resolve)
                .catch(reject);

        })

    }



    function runTestScript() {

        /// define the creoson objects to use
        let connObj = new creo.ConnectionObj();
        let creoObj = new creo.CreoObj();
        let utilObj = new creo.CreosonUtils();

        creoObj.filename = '*.asm';

        connObj.connect()
            .then(() => creoObj.list_files())
            .then((fileResObj) => utilObj.loopItems(fileResObj.filelist,getBomData))
            .catch((err) => {
                console.log('ERROR : '+err);
                alert(err);
            });

    }

 

 

 

Produces this output for each assembly toplevel :

 

 

--- TOP-LEVEL BOM DATA for : mhf2-16d1-m9nv.asm
     --> M9VERTICALTHREEWIRECONDUC-62473.PRT
     --> M9VERTICALTHREEWIRECONDUC-62474.PRT
     --> FINGER_SYNCHRONIZER_PLATE-62475.PRT
     --> FINGER_SYNCHRONIZER_PLATE-62476.PRT
     --> PORTCUT.PRT
--- TOP-LEVEL BOM DATA for : rcp2-ra4c-i-42p-10-100-p1-s-b.asm
     --> RCP2-RA4C-I-42P-10-100-P1-S-B_1.PRT
     --> RCP2-RA4C-I-42P-10-100-P1-S-B_2.PRT
     --> RCP2-RA4C-I-42P-10-100-P1-S-B_3.PRT
--- TOP-LEVEL BOM DATA for : na2-n12d.asm
     --> NA2-N12D_1.PRT
     --> NA2-N12D_2.PRT
     --> NA2-N12D_3.PRT
--- TOP-LEVEL BOM DATA for : na2-n12p.asm
     --> NA2-N12P_1.PRT
     --> NA2-N12P_2.PRT
     --> NA2-N12P_3.PRT
...

 

 

You can do the exact same process shown above with other languages using the same JSON transaction command : function and options - or natively without CRESON. 

 

NOTE : We have NO ETA on the release of the JavaScript-specific library for CREOSON... anyone who wants to be a beta tester can just reach out via private message.  But I think it is far enough along to show some working examples like above.

 

Hope that helps/inspires...

 

Dave

Announcements


Top Tags