Creating a new DraftView

Creating a new DraftView

t.galland
Contributor Contributor
629 Views
4 Replies
Message 1 of 5

Creating a new DraftView

t.galland
Contributor
Contributor

Hey,

 

I'm trying to create a DraftingView so I used this code :

 

 

using (var tx = new Transaction(document, "Create View"))
{
    tx.Start();
    var viewDraftType = new FilteredElementCollector(document)
          .OfClass(typeof(ViewFamilyType))
          .Cast<ViewFamilyType>()
          .FirstOrDefault(q => q.ViewFamily == ViewFamily.Drafting);

    if (viewDraftType == null)
    {
        tx.RollBack();
        return;
    }
    var draftView = ViewDrafting.Create(document, viewDraftType.Id);
    if (draftView == null)
    {
        tx.RollBack();
        return;
    }
    draftView.Name = $"{Init.Gui.MainView.ChartTitle}_Histogramme";
    draftView.Scale = 1;
tx.Commit(); }

 

 I tried with this post but I got a null value with the filterdelementcollector. Any idea ?

 

Thank you !

0 Likes
Accepted solutions (2)
630 Views
4 Replies
Replies (4)
Message 2 of 5

FAIR59
Advisor
Advisor
Accepted solution

You can use the default ViewTypeDrafting:

 

var draftView = ViewDrafting.Create(document, document.GetDefaultElementTypeId(ElementTypeGroup.ViewTypeDrafting));
Message 3 of 5

t.galland
Contributor
Contributor

I tried your code but I got this error (using a Try/catch) :

 

viewFamilyTypeId is not a valid ViewFamilyType for a drafting view.
Parameter name: viewFamilyTypeId

 

EDIT :

I tried with a new empty revit file and it worked. Can the file be "draftview lock" ?

0 Likes
Message 4 of 5

t.galland
Contributor
Contributor

I tried differents tricks and here is what I got. My project doesn't have DraftingView familly. When I manually create a DraftingView it create a detail view...

I'll search how to fixe that.

If you have any idea ?

 

Thank you !

0 Likes
Message 5 of 5

Revitalizer
Advisor
Advisor
Accepted solution

Hi,

 

you could use ElementTransformUtils.CopyElements to get the necessary type from another document.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine