Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Issues exporting collection of 3d views, based on name, to DWG.

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
jregan
508 Views, 5 Replies

Issues exporting collection of 3d views, based on name, to DWG.

I'm having problems exporting a group of 3d views to DWG based on their name containing a specified string. Below is the code that I'm using in my addin, and it seems to have a problem with the export method.

 

When I run the entire code, it get an error that says non empy list of views must be provided.

 

If I comment out the export method, the code runs through and I can see in the success dialog box that I'm getting the views I need filtered from the collection and their ID's, so I'm not sure why the export method is not reading the collection.

 

If I comment out the if statement, all the views in the project get exported and I get an error saying some were not printable.

 

Can someone help me understand why the export method is not working?

 

 

{
Document doc = commandData.Application.ActiveUIDocument.Document;
string docName = commandData.Application.ActiveUIDocument.Document.Title.ToString();
string title = docName.Replace(".rvt", "");

string folderName = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString() + "\\" + title + " Coordination Exports" + DateTime.Now.ToString(" yyMMdd") + "\\";

FilteredElementCollector collection = new FilteredElementCollector(doc).
OfClass(typeof(View3D));
DWGExportOptions opt = new DWGExportOptions();
string viewNames = "";

if (Directory.Exists(folderName))
{
Array.ForEach(Directory.GetFiles(folderName), File.Delete);
}
else
{
Directory.CreateDirectory(folderName);
}


foreach (View3D vt in collection)
{

string viewName = vt.Name;
ViewSet viewsToExport = new ViewSet();


if (viewName.Contains("xxxxxxxx"))
{
viewsToExport.Insert(vt);
}

ICollection<ElementId> exportViews = new List<ElementId>();

foreach (View view in viewsToExport)
{
viewNames += vt.Name + "" + view.Id.ToString() + Environment.NewLine;
exportViews.Add(view.Id);

}

doc.Export(folderName, viewName, exportViews, opt);
}

TaskDialog td = new TaskDialog("Coordination Exports");
td.MainContent = "All of the Coordination Exports in Project:" + Environment.NewLine +
docName + Environment.NewLine + viewNames + Environment.NewLine +
"Have been exported to: " + Environment.NewLine +
folderName + Environment.NewLine;
td.MainInstruction = "Coordination Views have been exported.";

td.Show();
return Result.Succeeded;
}
}
}

 

5 REPLIES 5
Message 2 of 6
arnostlobel
in reply to: jregan

jregan:

 

your code at lines 28 through 34 is not correct. I assume you meant that block to be out of the outer foreach loop. However, since it now sits inside of it, whatever you do between lines 28 and 34 only applies to the 3d-view currently used in the outer foreach loop. If that current view does not follow the naming pattern you look for, you do not add it to the viewsToExport, thus later the second (inner) foreach loop is meaningless as it would never add anything to the exportViews collection. Because of that, the collection remains empty when passed to the Export method, hence Revit complains.

 

By the way, I am having trouble trying to pair all the curly brackets in your sample. Did you, by any chance, omit some of the brackets when posting the question?

 

Arnošt Löbel
Message 3 of 6
jregan
in reply to: arnostlobel

Arnost,

Thank you for your reply. I'm sure you can tell, I'm a bit new to the API. I believe some of the brackets got omitted when I uploaded the file.

The code at lines 28 through 34 is meant to evaluate each that specific view in the collection and insert only the views that pass the if statement. Once I have a filtered ViewSet, I want to insert their ID's into the ElementId collection to create the batch for exporting.

When I comment out the export method, the task dialog displays only the views that pass the if statement and their Id's. If I can pull the Id's and the view names from the collection, why is the export method not able to find any Id's in the collection?

I'll try and upload the entire code shortly.
Message 4 of 6
jregan
in reply to: jregan

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit;
using Autodesk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace DWG_Exporter
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class DWG_Exporter : IExternalCommand
{
static AddInId appID = new AddInId(new Guid("772C0334-6B0C-4D64-8F0A-885C4D49FA5C"));
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
string docName = commandData.Application.ActiveUIDocument.Document.Title.ToString();
string title = docName.Replace(".rvt", "");

string folderName = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString() + "\\" + title + " Coordination Exports" + DateTime.Now.ToString(" yyMMdd") + "\\";

FilteredElementCollector collection = new FilteredElementCollector(doc).
OfClass(typeof(View3D));
DWGExportOptions opt = new DWGExportOptions();
string viewNames = "";

if (Directory.Exists(folderName))
{
Array.ForEach(Directory.GetFiles(folderName), File.Delete);
}
else
{
Directory.CreateDirectory(folderName);
}


foreach (View3D vt in collection)
{

string viewName = vt.Name;
ViewSet viewsToExport = new ViewSet();


if (viewName.Contains("xxxxxxxx"))
{
viewsToExport.Insert(vt);
}

ICollection<ElementId> exportViews = new List<ElementId>();

foreach (View view in viewsToExport)
{
viewNames += vt.Name + "" + view.Id.ToString() + Environment.NewLine;
exportViews.Add(view.Id);

}

//doc.Export(folderName, viewName, exportViews, opt);
}

TaskDialog td = new TaskDialog("Coordination Exports");
td.MainContent = "All of the Coordination Exports in Project:" + Environment.NewLine +
docName + Environment.NewLine + viewNames + Environment.NewLine +
"Have been exported to: " + Environment.NewLine +
folderName + Environment.NewLine;
td.MainInstruction = "Coordination Views have been exported.";

td.Show();
return Result.Succeeded;
}
}
}

Message 5 of 6
arnostlobel
in reply to: jregan

Please follow my chain of thought:

1. Say you have just gotten to the foreach loop at the line 36 (in your latest code sample)
2. Now you get the first view in the loop, let's say it happens to have name "aaaa"
3. You create the viewsToExport ViewSet at line 39 - it is empty it this point.
4. Since the name does not match your "xxxxx" pattern, you skip the if block on lines 41 through 44 (thus viewsToExport remains empty)
5. You create exportViews collection at line 45; it starts as empty.
6. The next foreach loop (line 46 through 50) is skipped entirely, because you did not add anything to viewsToExport.
7. Then on line 51, when you try to call Export, Revit complains because your exportViews is still empty.

Conclusions:
Your code would only work if all 3D views match the "xxxx" pattern. As long as there is one view that does not match it, which is very likely, it will not work. I suggest you try debugging it to see how it works for yourself (I have not done that - I am just looking at the raw code, but I think I am correct on this one.)
Arnošt Löbel
Message 6 of 6
jregan
in reply to: arnostlobel

You were 100% correct. Moving the location of my if statement allowed the code to run properly. Thank you for your help

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community