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?
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
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
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.