"ImageType.Create" method causes unexpected internal error.

"ImageType.Create" method causes unexpected internal error.

yongjoon4VB45
Participant Participant
1,699 Views
9 Replies
Message 1 of 10

"ImageType.Create" method causes unexpected internal error.

yongjoon4VB45
Participant
Participant

Hi, 

 

I'm trying to import an image using Revit 2021 API. 

 

from rpw import DB

image_options = DB.ImageTypeOptions("C:\\temp\\response.jpg", False, DB.ImageTypeSource.Import)
image_type = DB.ImageType.Create(doc, image_options)

 

However, it creates this error:

Exception : Autodesk.Revit.Exceptions.InternalException: Failed to create ImageType due to internal error.

 

Can someone help me understand why it's happening?

 

Thanks.

0 Likes
1,700 Views
9 Replies
Replies (9)
Message 2 of 10

franciscopossetto
Advocate
Advocate

Hey,

 

You need to add a new transaction to run ImageType.Create method. Then your code should work.

Github:
https://github.com/franpossetto
0 Likes
Message 3 of 10

yongjoon4VB45
Participant
Participant

Hi @franciscopossetto , 

 

Thanks so much for the response. I probably should have included the full code snippet! I did open the transaction, but it didn't really work.. Could you comment on my code?

 

with db.Transaction("Import Google Maps"):
    # Import Options
    active_view = uidoc.ActiveView
    image_options = DB.ImageTypeOptions(
        "C:\\temp\\response.jpg", False, DB.ImageTypeSource.Import
    )
    image_options.Resolution = 72
    image_type = DB.ImageType.Create(doc, image_options)
    image_placement = DB.ImagePlacementOptions(
        DB.XYZ(0, 0, 0), DB.BoxPlacement.BottomLeft
    )
    image_instance = DB.ImageInstance.Create(
        doc, active_view, image_type.Id, image_placement
    )

 

0 Likes
Message 4 of 10

Sean_Page
Collaborator
Collaborator

Doesn't look like you are supplying the correct info to the transaction.

 

trans = db.Transaction(doc,"name")
trans.Start()
##Do code
trans.Commit()
Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 5 of 10

yongjoon4VB45
Participant
Participant

Hi @Sean_Page ,

 

Thank you so much for your comment! I think that was one part of the issue. However, now I get this Revit error message along with the same 'internal error' response in the console.

Screen Shot 2021-04-23 at 6.49.24 PM.png

 

Could you provide any other insight here? 

 

Thanks so much!

0 Likes
Message 6 of 10

Sean_Page
Collaborator
Collaborator

Is there a reason why you use "db" for the transaction and then"DB" for the image? Are they different references?

 

Also, active view comes from the doc, not uidoc so that could also be a problem.

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 7 of 10

franciscopossetto
Advocate
Advocate

Hey,

 

I tested your code. I adeed the Transaction and it worked. 

t = DB.Transaction(doc, 'Import from GoogleMaps')

# Import Options
active_view = uidoc.ActiveView
image_options = DB.ImageTypeOptions(
    "C:\\temp\\1.jpg", False, DB.ImageTypeSource.Import
)
image_options.Resolution = 72
image_placement = DB.ImagePlacementOptions(
    DB.XYZ(0, 0, 0), DB.BoxPlacement.BottomLeft
)

t.Start()
image_type = DB.ImageType.Create(doc, image_options)
image_instance = DB.ImageInstance.Create(
    doc, active_view, image_type.Id, image_placement
)
t.Commit()

 

@Sean_Page  I thought the same thing, but UIDocument has the property ActiveView as well. Both return the Active View, but from what I could read on the Remarks, they have small differences.

Github:
https://github.com/franpossetto
Message 8 of 10

Sean_Page
Collaborator
Collaborator

I think the thing here is the UIDoc.ActiveView will always be the current UI document, but that may not be the doc you want to get the active view of. I'd assume in many cases these will work the same, but they are not always the same.

 

https://www.revitapidocs.com/2022/b6adb74b-39af-9213-c37b-f54db76b75a3.htm

 

https://www.revitapidocs.com/2022/043960ac-dde4-0f45-249f-8161646a4362.htm

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
Message 9 of 10

franciscopossetto
Advocate
Advocate

Interesting, thanks for sharing!

 

I have found a small but very good difference between them: ActiveView on UI Document provides get and set methods unlike Document which can only provides get. Last time I needed to open or switch between views I used uiDoc.RequestViewChange(view) what basically do the same that uiDoc.ActiveView = view.

 

Now I wonder if there is any difference between them or if they are mostly the same.

Github:
https://github.com/franpossetto
Message 10 of 10

yongjoon4VB45
Participant
Participant

Hi @franciscopossetto @Sean_Page ,

 

Thanks for your brainpower on this, and I finally solved the issue.  Thanks for testing the code! I found that it's not actually the issue of the code itself, but the JPG image. I'm currently making an HTTP request from Revit to Google Maps API to bring the JPG image, and for some reason the JPG response data from GMap API is not readable in Revit API environment. Other response type such as PNG or BMP has no issue with the execution with the same code.

 

Maybe I should have shared the full context of the code and where the source image came from, but this is what I 've learned so far! Hope it helps you in the future when you work on something similar.

 

Thanks! 

0 Likes