Jump to content

How to repeat a part of script for all the stages


---
 Share

Recommended Posts

Hello,

 

I have written a script that make automatically a comparison between new part and old part. it works very well and the last step is to make it run for all stages automatically. I think it's possible, but I don't know how. The red rectangle is the part I have to run for all the stages. And this is the code I written.

# -*- coding: utf-8 -*-

import gom

start_stage = ('stage')

for current_stage in gom.app.project.stages:
	
	gom.script.sys.show_stage (stage=current_stage)
	
	if gom.app.project.get ('stage') == start_stage:
		continue

reportPages = []



for t in gom.app.project.reports:
	reportPages.append(t.name)

gom.script.mesh.convert_mesh_to_cad_data (
	meshes=[gom.app.project.clipboard.actual_elements[0]], 
	replace_in_part=gom.app.project.parts[0])

gom.script.sys.show_stage (stage=gom.app.project.stages[0])

gom.script.sys.switch_to_report_workspace ()

gom.script.sys.switch_to_report_workspace ()

gom.interactive.report.update_report_page (
	pages=[t], 
	used_alignments='report', 
	used_digits='report', 
	used_legends='report', 
	used_stages='current', 
	used_units='report')
	
gom.interactive.sys.save_project_as (
	
	open_in_presentation_mode=False)

gom.interactive.report.export_pdf (
	export_all_reports=True, 
	
	jpeg_quality_in_percent=100)
	
gom.script.sys.switch_to_inspection_workspace ()

gom.script.sys.delete_stage (stages=gom.StageSelection (first=gom.app.project.stages[0], last=gom.app.project.stages[0]))

Thank you very much.

2022-05-18 11_23_20-Clipboard.png

Link to comment
Share on other sites

In python:

To repeat elements inside a python loop use a indent (tab).

All elements within indent/tab are repeated.

Test Python (online) here:

https://trinket.io/embed/python3

Logical only pure python works online. The GOM features of course not!

Be careful:

Some instructions might delete or correct operating system! Take care of backups. Do not try and copy all code find online in a desperate frenzy. Use PIP installs only if you know why, not by think I want. Also take care using sys commands in pyhton.

 

for i in range(10):
	print("Inside Loop always with tab or indent")
	print("repeat: " +str(i+1))

print("this is outside loop")

 

Edited
Link to comment
Share on other sites

Please sign in to view this quote.

Thank you very much! I've done it, but I have 12 stages and it runs well until the 6th stage, and then it stops and show me the error in the image.

Thhis is my code:

# -*- coding: utf-8 -*-

import gom

reportPages = []

for t in gom.app.project.reports:
	reportPages.append(t.name)
	
for i in range(len(gom.app.project.stages)):
	gom.script.sys.show_stage (stage=gom.app.project.stages[i])

	gom.script.mesh.convert_mesh_to_cad_data (
	meshes=[gom.app.project.clipboard.actual_elements[0]], 
	replace_in_part=gom.app.project.parts[0])

	gom.script.sys.show_stage (stage=gom.app.project.stages[0])

	gom.script.sys.switch_to_report_workspace ()

	gom.script.sys.switch_to_report_workspace ()

	gom.interactive.report.update_report_page (
	pages=[t], 
	used_alignments='report', 
	used_digits='report', 
	used_legends='report', 
	used_stages='current', 
	used_units='report')
	
	gom.interactive.sys.save_project_as (
	
	open_in_presentation_mode=False)

	gom.interactive.report.export_pdf (
	export_all_reports=True, 
	
	jpeg_quality_in_percent=100)
	
	gom.script.sys.switch_to_inspection_workspace ()

	gom.script.sys.delete_stage (stages=gom.StageSelection (first=gom.app.project.stages[0], last=gom.app.project.stages[0]))

 

CopyQ.qMlwBI.png

Link to comment
Share on other sites

Why did you type range and [i] in the code? I am not sure the stages object is addressed numerical? It looks like you are taking the string length of a stagename and index on that? 

for i in range(len(gom.app.project.stages)):
	gom.script.sys.show_stage (stage=gom.app.project.stages[i])

That example I gave with the for loop and range is elementary python using indents with for loop. That has nothing to do with your code.

You original code is (stick with that if it is working):

for current_stage in gom.app.project.stages:
	
	gom.script.sys.show_stage (stage=current_stage)
	
	if gom.app.project.get ('stage') == start_stage:
		continue

I am not sure if I can help you further. For Python questions there are other forums available, with smarter people then me. Be careful with building loops I see you use a delete command. If you are not sure what you are doing you might loose measurement data.

 

Edited
Link to comment
Share on other sites

Sorry checked it, your code with range and len seems to work. But I do not know why you do that and it is strange.

Not sure how I can help.

# -*- coding: utf-8 -*-

import gom

for current_stage in gom.app.project.stages:
	print(current_stage)
	
print("")

for i in range(len(gom.app.project.stages)):
	print(gom.app.project.stages[i])

 

Edited
Link to comment
Share on other sites

I understand why: in the first run it counts 12 range, at the end of the first run it delete the first stage, so in the second run i=11 and so on. At the sixth run i is 6, so, it stops. I think that the problem is that I delete in every run the first stage [0] so so when it come to the sixth run i is egual to the number of the stages.

Link to comment
Share on other sites

If you loop over stages names and not numbers?

Like I told I think it is strange what you do: range(len(.stages)) looping over changes stages to numbers and change it back to name. The same result with: no numbers involved only (unique) names:

for current_stage in gom.app.project.stages:
	print(current_stage)

 

Edited
Link to comment
Share on other sites

Please sign in to view this quote.

I don't think I understand.. I want to use this script with all my project, I can not use unique names.

Link to comment
Share on other sites

 

Both loops below do the same thing show a stage.

Somewhere you changes your code to:  range(len(gom.app.project.stages)) and loop over stages by number not by object.

Why you did that I do not know, it is not logical and strange. Al I say: try the loop as you initial intended (first one in code below).

I think I gave all the help possible,

good luck, Vince

 

# -*- coding: utf-8 -*-

import gom

for current_stage in gom.app.project.stages:
	print(current_stage)
	gom.script.sys.show_stage (stage=current_stage)
	
print("")

for i in range(len(gom.app.project.stages)):
	print(gom.app.project.stages[i])
	gom.script.sys.show_stage (stage=gom.app.project.stages[i])

 

Edited
Link to comment
Share on other sites

Thanks to everyone, finally I have made my script! Monday I will post here the code and what it do, if someone is interested to.

Link to comment
Share on other sites

 Share

×
×
  • Create New...