Viewport.Create() returns null for drafting view

Viewport.Create() returns null for drafting view

Anonymous
Not applicable
2,176 Views
10 Replies
Message 1 of 11

Viewport.Create() returns null for drafting view

Anonymous
Not applicable

I'm working on an addin that creates multiple drafting views (each one containing a TextNote) before placing them on a ViewSheet through the Viewport.Create() method. 

I only call Viewport.Create() under the condition that Viewport.CanAddViewToSheet(Document, ViewSheet.Id, ViewDrafting.Id) returns true for the ViewSheet and a given ViewDrafting element in my list; this method returns true for all of them. 

Strangely enough, the list's first ViewDrafting element (which I should be able to add to the sheet according to the above method) creates a null Viewport when passed into the Viewport.Create() method; the remaining ViewDrafting elements create successful Viewports as expected.

 

I read the documentation for the Viewport class, but have yet to find anything suggesting how a Viewport could be null when the ViewDrafting element and ViewSheet element meet the criteria of the CanAddViewToSheet() method. 

Is there an undocumented condition where the Viewport.Create() method returns null?

Accepted solutions (1)
2,177 Views
10 Replies
Replies (10)
Message 2 of 11

a-carpentier
Advocate
Advocate
Accepted solution

Would you mind posting your code here so that we can have a better understanding of the situation?

Have you tried regenerating the document after creating the drafting view but before placing them on a view sheet?
I remember having a similar issues with family types being created but that were not usable until I regenerated my document.

Message 3 of 11

Anonymous
Not applicable

Thanks, @a-carpentier. I had been regenerating the document after creating each viewport, but forgot to regenerate before creating a viewport of the first Viewdrafting element. It makes perfect sense that this would happen, as I was deleting an element from the document prior to the first Viewport.Create() call. This is just another case of forgetting to double-check my ground with doc.Regenerate().

Thanks again!

0 Likes
Message 4 of 11

Maltezc
Advocate
Advocate

Mine is still returning null. did you have any other solutions for it?

 

Here is a code snippet:

 

 

 

 

 

if (colTitleBlocks != null)
                {
                    LoadTitleBlocks loadFamily = new LoadTitleBlocks();
                    loadFamily.loadTitleBlocks(commandData);
                }

                // grab first as Example
                FamilySymbol firstTitleBlock = colTitleBlocks.FirstElement() as FamilySymbol;


ViewSheet viewSheet = ViewSheet.Create(doc, firstTitleBlock.Id);
doc.Regenerate();

doc.Regenerate();
ViewDrafting viewDrafting = ViewDrafting.Create(doc, viewFamilyType.Id);
ElementId viewDraftingId = viewDrafting.Id;
doc.Regenerate();

UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2, (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);

FilteredElementCollector colDraftingViews = new FilteredElementCollector(doc).OfClass(typeof(ViewDrafting));

// cast Collector to List
List<ViewDrafting> viewDraftingViewsList = colDraftingViews.Cast<ViewDrafting>().ToList();

// grab first as example
ViewDrafting firstDraftingView = viewDraftingViewsList[0];


// Create Viewport
doc.Regenerate();
Viewport newViewPort = Viewport.Create(doc, viewSheet.Id, firstDraftingView.Id, new XYZ(location.U, location.V, 0));
doc.Regenerate();

 

 

 

 

 

 

0 Likes
Message 5 of 11

a-carpentier
Advocate
Advocate

@Maltezc, Could it be that you haven't defined "firstDraftingView" ?
In your code snippet you only defined "ViewDrafting" and "ViewDraftingId".

0 Likes
Message 6 of 11

Maltezc
Advocate
Advocate

@a-carpentier , Thank you for the swift reply. I've updated my code. 

 

I don't think its that although it could be the type. I am able to place a ViewPlan with similar code however drafting views seem to be different. 

0 Likes
Message 7 of 11

a-carpentier
Advocate
Advocate

I just did a quick test, and I think I know what's going on.

I manually created a drafting view and tried to put it on a sheet, and I got an error message:

 

Capture.PNG

 

I think that may be what's going on for you as well. Try placing a detail line or a detail item on your drafting view before creating the viewport, that may solve your problem.

Message 8 of 11

Maltezc
Advocate
Advocate

Yes, I have loaded in a detail item. a simple break line.  I can create the view and place a detail item inside the view however the button breaks when i create to create a viewport. 

 

What happens when you place a detail item inside it and then use viewport.create?

0 Likes
Message 9 of 11

Maltezc
Advocate
Advocate

@a-carpentier , figured it out. 

 

You were right. Mentally, I had added an element before creating the viewport. but it turns out in actuality, I was trying to add a detail component after creating the viewport. 

 

Tried to pull a cart-before-the-horse trick. Revit didn't like it. 

 

Thanks for the help!

0 Likes
Message 10 of 11

TripleM-Dev.net
Advisor
Advisor

Hi,

 

No need to call that many Doc.Regenerates.

There needs to be some geometry (a textnote, a line whatever) on the drawingsheet.

 

Workflow:

1. begin transaction

2. create ViewSheet and ViewDrafting

3. Create something on the ViewDrafting (a textnote for instance)

4. Call "Doc.Regenerate"

5. Create the viewport (Viewsheet and draftview created on 2)

6. commit.

 

if (colTitleBlocks != null)
{
LoadTitleBlocks loadFamily = new LoadTitleBlocks();
loadFamily.loadTitleBlocks(commandData);
}

// grab first as Example
FamilySymbol firstTitleBlock = colTitleBlocks.FirstElement() as FamilySymbol;


ViewSheet viewSheet = ViewSheet.Create(doc, firstTitleBlock.Id);
doc.Regenerate();

doc.Regenerate();
ViewDrafting viewDrafting = ViewDrafting.Create(doc, viewFamilyType.Id); // viewFamilyType => is it a ViewFamily.Drafting type?
ElementId viewDraftingId = viewDrafting.Id; // Not really needed

// Create something on the draftview..
doc.Regenerate();

UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2, (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);

 

//What's the lines below for??, the draftview was already create above? => viewDrafting

FilteredElementCollector colDraftingViews = new FilteredElementCollector(doc).OfClass(typeof(ViewDrafting));

// cast Collector to List
List<ViewDrafting> viewDraftingViewsList = colDraftingViews.Cast<ViewDrafting>().ToList();

// grab first as example
ViewDrafting firstDraftingView = viewDraftingViewsList[0];


// Create Viewport
doc.Regenerate();
Viewport newViewPort = Viewport.Create(doc, viewSheet.Id, firstDraftingView.Id viewDrafting.Id, new XYZ(location.U, location.V, 0));
doc.Regenerate();

 

- Michel.

Message 11 of 11

danielJY6FB
Contributor
Contributor

I was having this same problem with a plan view. What finally fixed it for me was making sure the view's Crop Box parameter was turned on when placing it onto the sheet. (BuiltInParameter.VIEWER_CROP_REGION)

The three lines of code that matter are below (In my code, actually happen in different but sequential transactions)

view.get_Parameter(BuiltInParameter.VIEWER_CROP_REGION).Set(1);
bool canAddViewToSheett = Viewport.CanAddViewToSheet(doc, sheetId, viewId); // returns true, regardless of weather Viewport.Create() returns a Viewport or returns just null ... weird
Viewport newViewport = Viewport.Create(doc, sheetId, viewId, centerOfViewport);



The odd thing to me is that, whether or not the Viewport.Create() would return null, the Viewport.CanAddViewToSheet() would always return true - does anyone know why that might be?

0 Likes