How to change target file of NSC bitmap wizard by using Python

  • 16 March 2024
  • 3 replies
  • 37 views

Hi, 
Is there any one know that how to change the target file in the NSC Bitmap Wizard of merit function editor by using Python?


3 replies

Userlevel 7
Badge +2

@Cheng-Mu Tsai

 

I think to every image file is associated an integer ID and you have to use this number to update the property BitmapFile of the MFE.NSCBitmapWizard before applying the wizard. To figure out what the ID is, as far as I know, you have to loop through all the different files. I made a small example below:

# Function to retrieve an image ID based on an image file name (including extension)
def find_first_image_id_by_name(bitmap_wizard, image_name):
for image_id in range(bitmap_wizard.NumberOfBitmapFiles):
if image_name.lower() == bitmap_wizard.GetBitmapFileAt(image_id).lower():
return image_id

return -1


# NSC Bitmap Wizard
bitmap_wizard = TheSystem.MFE.NSCBitmapWizard

# Retrieve image ID based on an image file name (including extension)
image_name = 'square.bmp'
image_id = find_first_image_id_by_name(bitmap_wizard, image_name)

# If image not found terminate script
if image_id == -1:
print('Image not found!')
exit()

# Otherwise, apply the new image to the wizard
bitmap_wizard.BitmapFile = image_id
bitmap_wizard.Apply()

Let me know if this is clear and works for you.

PS: you may need to clear or perform additional operations on the Merit Function so the operands are added properly to your file.

Take care,

 

David

Dear David, 
Thanks for your answer. 
If I am sure that the file is in the folder IMAFiles, 
could we set BitmapFile like 
bitmap_wizard.BitmapFile = image_name ?

 

best wishes, 

CM tsai

Userlevel 7
Badge +2

@Cheng-Mu Tsai

 

To the best of my knowledge that won’t work because BitmapFile is an integer as indicated by the int on the left-hand side of the Syntax Help file:

If you try to use a string you will get a:

TypeError: an integer is required

Therefore you need to know the image ID from the OpticStudio perspective. The numbering seems to be somewhat consistent:

  • The ID starts at 0 and increases with the number of files
  • For the same file type, such as BMP, the files are ordered alphabetically
  • As far as I tested, JPG comes before BMP

For example, if you only have A.BMP, and B.BMP, they will be assigned IDs 0 and 1 respectively (A.BMP = 0, B.BMP = 1). If you have B.JPG, and A.BMP, they will also be assigned IDs 0 and 1 respectively (B.JPG = 0, A.BMP = 1), because of the file extension. I don’t know about the other file formats, you’d have to test that. However, you could use that logic to simplify how you retrieve the IDs.

I hope that makes sense.

Take care,

 

David

Reply