Export views to DWG

Export views to DWG

Anonymous
Not applicable
2,217 Views
4 Replies
Message 1 of 5

Export views to DWG

Anonymous
Not applicable

So I thought that I solved the puzzle but I keep getting it wrong. 

 

The code:

namespace Prototype.Forms
{
    /// <summary>
    /// Interaction logic for UserForm.xaml
    /// </summary>
    public partial class UserForm : UserControl
    {
        public Document UserDoc;
        public IList<View3D> viewList = new List<View3D>();

        public UserForm(Document doc, String switchCase)
        {
            InitializeComponent();
            FilteredElementCollector collector = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views);

            collector.OfClass(typeof(View3D));

            foreach (View3D v in collector) {
                if (v.CanBePrinted == true)
                {
                    viewList.Add(v);
                }
            }

            UserDoc = doc;

        private void Export_Click(object sender, RoutedEventArgs e)
        {
            foreach (View3D v in viewList)
            {
                if(v.CanBePrinted == true)
                {
                    DWGExport(UserDoc, v);
                }
            }
            
        }

        public void DWGExport(Document doc, View view)
        {
            DWGExportOptions opt = new DWGExportOptions();

            ICollection<ElementId> views = new List<ElementId>
            {
                view.Id
            };

            try
            {
                doc.Export(System.IO.Path.GetDirectoryName(doc.PathName), view.Name, views, opt);
            }
            catch (Exception e)
            {
                TaskDialog.Show("Error Caught", e.ToString());
            }
        }
    }

    public class DataItem
    {
        public Boolean Select { get; set; }
        public String ViewName { get; set; }
        public String Type { get; set; }
    }
}

In short I filter for View3D elements add them to the viewList then I've assigned a button to export all the View3D to DWG by way View3D ElementID. 

 

I do manage to export a view which happens to be the active view but not the rest of the views, I've also checked that the viewList.Count is greater then 1 and it is.

 

Any help would be appreciated!

 

0 Likes
Accepted solutions (1)
2,218 Views
4 Replies
Replies (4)
Message 2 of 5

rodrigohbm
Advocate
Advocate

Hello
I have the following code
made in vb.net
I hope it helps you

regards

 

Sub Button8Click(sender As Object, e As EventArgs) 'EXPORTA VIEWS A DWG
dim app as uiapplication = commanddata.application
dim uidoc as uidocument = app.activeuidocument
dim activedoc as document = uidoc.document

'get all the elements in the model database
dim collector2 as new filteredelementcollector(activedoc)


' filter out all elements except views
dim collection as icollection(of element) = collector2.ofclass(gettype(autodesk.revit.db.view)).toelements()
dim sheetstodwg as new list(of elementid)()


'save all view of model
For Each vie As element In collection
sheetstodwg.add(vie.id)
Next


Dim exportdwg As String = String.empty
Dim ruta As String = TextBox1.Text
Dim file As String = String.empty
Dim numer As String = String.empty

exportdwg = "AAMB_mm"

For Each index As Object In listView2.Items
Dim formato As [String] = index.ToString()
Dim s As String = formato.ToString
Dim t As String = s.Replace("ListViewItem:", "").Replace("{", "").Replace("}", "")

numer = (Mid(t.ToString, 1, InStr(1, t.ToString, "...")-1))
file = Mid(t.ToString, InStr(1, t.ToString, "...")+3)

For Each rabbit As Object In sheetstodwg
Dim todwg As New list(Of elementid)()
If (rabbit.ToString) = (Trim(numer.ToString)) Then
todwg.Add(rabbit)
activedoc.export(ruta, file, todwg, dwgexportoptions.getpredefinedoptions(activedoc, exportdwg))
End if
Next
Next
End Sub

 

0 Likes
Message 3 of 5

jeremytammik
Autodesk
Autodesk

Dear Erik,

 

Thank you for your query.

 

Do these examples help?

 

https://www.google.com/search?q=dwgexportoption&as_sitesearch=thebuildingcoder.typepad.com

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.47

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 4 of 5

Anonymous
Not applicable
Accepted solution

Solved the problem by adding the following in the try block in DWGExport method:

 

Essentially I switched the active view in the document then it worked... which I feel shouldn't be necessary.

UIDocument uiDOC = new UIDocument(UserDoc);
uiDOC.ActiveView = view;
0 Likes
Message 5 of 5

lwlXUTTT
Advocate
Advocate

This seeems to be a only half solution - there is definitely a Bug in Document.Export method from Revit side- having the set of various views, it always exports the ActiveView of UIDocument . (we are using Revit 2022.1)
Workaround to set the printed view as the ActiveView of UIDocument before executing Document.Export solves the problem, however it makes the exporting process 2x longer.

 

@jeremytammik- are you familiar with the issue? has anyone reported the issue already?

 

Any feedback much appreciated,

Lukasz

0 Likes