public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements) { try { Autodesk.Revit.UI.UIApplication revitApp = revit.Application; //get the active document ExternalCommandData cdata = revit; Document doc = cdata.Application.ActiveUIDocument.Document; //UIDocument uidoc = cdata.Application.ActiveUIDocument; //Selection selection = uidoc.Selection; //ElementSet collection = selection.Elements; //if (0 == collection.Size) //{ // // If no elements selected. // TaskDialog.Show("Revit", "You haven't selected any elements."); //} //else //{ // LuxExporter.Revit_Transform test = new Revit_Transform(collection); //} //get linked files DocumentSet LinkedFiles = revitApp.Application.Documents; //create a Luxexporter LuxExporter_Main Exporter = new LuxExporter_Main(); //set hard coded exporter output path Exporter.OutputFilePath = @"C:/temp/lux/"; //get list of 3d views in file Revit_Filter ViewFilter = new Revit_Filter(doc); Exporter.ViewToExport = ViewFilter.GetThreeDViews(); Exporter.CurrentRevitDocument = doc; Exporter.LinkedFiles = LinkedFiles; Exporter.ExportToLux(); return Autodesk.Revit.UI.Result.Succeeded; } catch (Exception) { return Autodesk.Revit.UI.Result.Failed; } }
// main function public void ExportToLux() { //loop through views and export to lux foreach (Autodesk.Revit.DB.View3D ExportView in v3DViewToExport) { // Create new stopwatch Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //set up list geoFile to include List<String>GeoFileList = new List<string>(); //get all elements in view FilteredElementCollector viewCollector = new FilteredElementCollector(vDoc, ExportView.Id); //i could add a filter for each category here ( or for specials like lights...) //viewCollector.OfCategory(BuiltInCategory.OST_Walls); //cast views elements to list IEnumerable<Element> ElementList = viewCollector.Cast<Element>(); //create scene folder //check whether folder allready exists //if not create scene folder //create PLY folder //create RES folder (textures & other data) if (Directory.Exists(vOutputFilePath + ExportView.Name.ToString())) { //delete folder?? } else { //create all directories //make sure view name has no illegal characters: {} //create scene Directory Directory.CreateDirectory(vOutputFilePath + ExportView.Name.ToString()); //create PLY folder Directory.CreateDirectory(vOutputFilePath + ExportView.Name.ToString() + "/" + "PLY"); //create resources folder Directory.CreateDirectory(vOutputFilePath + ExportView.Name.ToString() + "/" + "RES"); } //set up geofile path variable String sOutputPathGeoFile = ""; //setup PLY path variable String PLYDirectory = ""; //check for linked Revit Files visible in view LuxExporter.Revit_Filter Filter = new Revit_Filter(vDoc); List<Element> RevitLinks = Filter.GetRevitLinks(ExportView.Id); //setup the geomtry option for the current view Autodesk.Revit.DB.Options GeometryOption = new Options(); GeometryOption.ComputeReferences = true; GeometryOption.View = ExportView; //export linked files if (RevitLinks.Count > 0) { //count how many instances of each individual link exist //check whether any of these instances collides with section box if there is any //if collision no instance, if no collision or no section box instanciate whole file //export link file and instanciate as often as required //create dictionary storing linked file name and number of occurences Dictionary<String, int> DRevitLinks = new Dictionary<string, int>(); //list containing all links of models not cut by section box and their transformation data List<LuxExporter.Revit_Linked_Files> lWholeModelLinks = new List<Revit_Linked_Files>(); //list containing all links of models cut by section box and their transformation data List<LuxExporter.Revit_Linked_Files> lCutModelLinks = new List<Revit_Linked_Files>(); int LinkCounter = 0; //loop through link list and sort items before exporting foreach (Element LinkItem in RevitLinks) { //increase link Counter LinkCounter++; //remove stuff from name String LinkName = LinkItem.Name.ToString().Substring(0, LinkItem.Name.ToString().IndexOf(".rvt")); //flag for bounding box check Boolean BoundingBoxOK = true; //check whether bounding box active //pointer int NumberOfLinkOccurences = 0; if (ExportView.SectionBox.Enabled) { //if yes does link clash with box? //get boundingbox of item BoundingBoxXYZ ElementBounding = LinkItem.get_BoundingBox(ExportView); //get sectionbox Autodesk.Revit.DB.BoundingBoxXYZ ViewSectionBox = ExportView.SectionBox; //check whether element bounding box is completely enclosed in view bounding box if not disable instancing! LuxExporter.Revit_BoundingBox_Checker checker = new Revit_BoundingBox_Checker(); BoundingBoxOK = checker.BoundingBox_Checker(ElementBounding, ViewSectionBox); } //get the link transformation Instance inst = LinkItem as Instance; RevitLinkTransform = inst.GetTransform(); if (BoundingBoxOK) { //if no boundingbox and no clash //check whether link already in list if (lWholeModelLinks.Contains(new LuxExporter.Revit_Linked_Files(LinkName))) { //get link class and add transformation LuxExporter.Revit_Linked_Files dummyLink = new Revit_Linked_Files(LinkName); LuxExporter.Revit_Linked_Files ExLink = lWholeModelLinks.Find(xy => xy.LinkName == dummyLink.LinkName); ExLink.AddTransForm(RevitLinkTransform); } else { //create new link class LuxExporter.Revit_Linked_Files dummyLink = new Revit_Linked_Files(LinkName); //add transformation dummyLink.AddTransForm(RevitLinkTransform); //add link element dummyLink.RevitLink = LinkItem; //add link short name dummyLink.LinkShortName = LinkCounter.ToString(); //check if this link has been exported before if (DRevitLinks.TryGetValue(LinkName, out NumberOfLinkOccurences)) { //increase counter DRevitLinks[LinkName] = NumberOfLinkOccurences + 1; NumberOfLinkOccurences++; } else { //add link to dictionary DRevitLinks.Add(LinkName, 0); } //remove .rvt from link instance name PLYDirectory = vOutputFilePath + ExportView.Name.ToString() + "/PLY/" + LinkName + "_" + NumberOfLinkOccurences.ToString() + "/"; //store unique LinkName in class dummyLink.UniqueLinkName = LinkName + "_" + NumberOfLinkOccurences.ToString(); //store path in class dummyLink.PLYFolderPath = PLYDirectory; //create geometry file name String GeoFilePath = vOutputFilePath + ExportView.Name.ToString() + "/" + LinkName + "_" + NumberOfLinkOccurences.ToString() + "-geom.lxo"; dummyLink.GeoFilePath = GeoFilePath; //add to list lWholeModelLinks.Add(dummyLink); } } else { //file need to be exported again //create new link class LuxExporter.Revit_Linked_Files dummyLink = new Revit_Linked_Files(LinkName); //add transformation dummyLink.AddTransForm(RevitLinkTransform); //add link element dummyLink.RevitLink = LinkItem; //add link short name dummyLink.LinkShortName = LinkCounter.ToString(); //check if this link has been exported before if (DRevitLinks.TryGetValue(LinkName, out NumberOfLinkOccurences)) { //increase counter DRevitLinks[LinkName] = NumberOfLinkOccurences + 1; NumberOfLinkOccurences++; } else { //add link to dictionary DRevitLinks.Add(LinkName, 0); } //remove .rvt from link instance name PLYDirectory = vOutputFilePath + ExportView.Name.ToString() + "/PLY/" + LinkName + "_" + NumberOfLinkOccurences.ToString() + "/"; //store unique LinkName in class dummyLink.UniqueLinkName = LinkName + "_" + NumberOfLinkOccurences.ToString(); //store path in class dummyLink.PLYFolderPath = PLYDirectory; //create geometry file name String GeoFilePath = vOutputFilePath + ExportView.Name.ToString() + "/" + LinkName + "_" + NumberOfLinkOccurences.ToString() + "-geom.lxo"; dummyLink.GeoFilePath = GeoFilePath; //add to list lCutModelLinks.Add(dummyLink); } } //combine linked model lists into one list List<LuxExporter.Revit_Linked_Files> lModelsCombined = new List<Revit_Linked_Files>(); lModelsCombined.AddRange(lWholeModelLinks.ToArray()); lModelsCombined.AddRange(lCutModelLinks.ToArray()); try { //loop through all model links and export them //foreach (LuxExporter.Revit_Linked_Files item in lWholeModelLinks) foreach (LuxExporter.Revit_Linked_Files item in lModelsCombined) { //create PLY output directory Directory.CreateDirectory(item.PLYFolderPath); //get elements in link foreach (Document ditem in vLinkedFiles) { //check for match in name if (ditem.PathName.Contains(item.LinkName)) { //export materials if (FilteredElementCollector.IsViewValidForElementIteration(ditem,ExportView.Id)) { //get all elements in view FilteredElementCollector LinkViewCollector = new FilteredElementCollector(ditem, ExportView.Id); //cast views elements to list IEnumerable<Element> LinkElementList = LinkViewCollector.Cast<Element>(); //export link GeoFileList.Add(ExportElementList(LinkElementList, ExportView, GeometryOption, item.GeoFilePath, item.PLYFolderPath, item)); } else { //get model element filter LuxExporter.Revit_Filter FilterUtility = new Revit_Filter(); //this filter also select non placed items!!! Autodesk.Revit.DB.LogicalOrFilter ModelFilter = FilterUtility.BuildFullModelFilter(); //filter elements from linked file Autodesk.Revit.DB.FilteredElementCollector collector = new FilteredElementCollector(ditem); //filter out types collector.WherePasses(ModelFilter).WhereElementIsNotElementType(); //pass eklement list IEnumerable<Element> LinkElementList =collector.Cast<Element>(); //export link GeoFileList.Add(ExportElementList(LinkElementList, ExportView, GeometryOption, item.GeoFilePath, item.PLYFolderPath, item)); } } } } } catch (Exception ex) { MessageBox.Show("Test"+ex.Message); throw; } } //reset the link transform RevitLinkTransform = null; sOutputPathGeoFile = vOutputFilePath + ExportView.Name.ToString() + "/" + ExportView.ViewName.ToString() + "-geom.lxo"; PLYDirectory = vOutputFilePath + ExportView.Name.ToString() + "/PLY/"; //export host file GeoFileList.Add(ExportElementList(ElementList, ExportView, GeometryOption, sOutputPathGeoFile,PLYDirectory,null)); // Stop timing stopwatch.Stop(); MessageBox.Show("Time elapsed: {0}" + stopwatch.Elapsed); //create material outputfilepath String sOutputPathMatFile = vOutputFilePath + ExportView.Name.ToString() + "/" + ExportView.ViewName.ToString() + "-mat.lxm"; try { //global::System.Windows.Forms.MessageBox.Show("Test"); //write out materials file //get materials in view only -->> does not work! //MaterialSet LMaterials = ExportView.Materials; //get materials in project LuxExporter.Revit_Filter MaterialFilter = new Revit_Filter(vDoc); List<Material> LMaterials = MaterialFilter.GetAllMaterials(); //setup helper class LuxExporter.Revit_Material_Worker MaterialExporter = new Revit_Material_Worker(vDoc); //setup export String StringBuilder MaterialExport = new StringBuilder("# Lux Render CVS - Material File"); MaterialExport.AppendLine("# Exported by LuxRev 0.2- ALPHA"); MaterialExport.AppendLine("# View Name: " + ExportView.ViewName.ToString()); MaterialExport.AppendLine(""); //add glass2 presets: //World MaterialExport.AppendLine("MakeNamedVolume \"World\" \"homogeneous\""); MaterialExport.AppendLine("\"float fresnel\" [1.000292658805847]"); MaterialExport.AppendLine("\"color g\" [-0.30000001 -0.30000001 -0.30000001]"); MaterialExport.AppendLine("\"color sigma_a\" [0.00000000 0.00000000 0.00000000]"); MaterialExport.AppendLine("\"color sigma_s\" [0.02500000 0.02500000 0.02500000]"); //Glass MaterialExport.AppendLine("MakeNamedVolume \"Glass\" \"clear\""); MaterialExport.AppendLine("\"float fresnel\" [1.519999980926514]"); MaterialExport.AppendLine("\"color absorption\" [0.01614268 0.00300774 0.03046782]"); //loop through all materials and export foreach (Material RevitMaterial in LMaterials) { MaterialExport.Append(MaterialExporter.ExportMaterial(RevitMaterial)); } //write out materials file using (System.IO.StreamWriter MatFile = new System.IO.StreamWriter(sOutputPathMatFile)) { MatFile.WriteLine(MaterialExport); } } catch (Exception) { MessageBox.Show("Failed To write mat file"); throw; } //view utility class LuxExporter.Revit_View_ThreeD ViewData = new Revit_View_ThreeD(ExportView); //get the camera data XYZ CameraPosition = ViewData.CameraPosition; XYZ CameraViewDirection = ViewData.CameraViewDirection; XYZ CameraUp = ViewData.CameraUp; //get the screenwindow date //assume resolution of 800 x 600 //lens shift is 0 for x and y //scale as per view double dScreenaspectRatio = (800.00 / 600.00); LuxExporter.LuxExporter_ScreenWindow ScreenWindow = new LuxExporter_ScreenWindow(dScreenaspectRatio, 0, 0, ExportView.Scale); Double[] vScreenWindow = ScreenWindow.GetScreenWindow(); //create string for header file - mainly hardcoded as of yet StringBuilder LXS = new StringBuilder("# Lux Render CVS - Scene File"); LXS.AppendLine("# Exported by LuxRev 0.2- ALPHA"); LXS.AppendLine("# View Name: "+ ExportView.ViewName.ToString()); LXS.AppendLine("LookAt "+CameraPosition.X.ToString("f6")+" "+CameraPosition.Y.ToString("f6")+" "+CameraPosition.Z.ToString("f6")); LXS.AppendLine(" " + CameraViewDirection.X.ToString("f6") + " " + CameraViewDirection.Y.ToString("f6") + " " + CameraViewDirection.Z.ToString("f6")); LXS.AppendLine(" "+CameraUp.X.ToString("f6")+" "+CameraUp.Y.ToString("f6")+" "+CameraUp.Z.ToString("f6")); LXS.AppendLine(""); LXS.AppendLine("Camera \"perspective\""); LXS.AppendLine(" \"float fov\" [150]"); LXS.AppendLine(" \"float hither\" [0]"); LXS.AppendLine(" \"float yon\" [10000000]"); LXS.AppendLine(" \"float lensradius\" [0]"); LXS.AppendLine(" \"bool autofocus\" [\"true\"]"); LXS.AppendLine(" \"float shutteropen\" [0]"); LXS.AppendLine(" \"float shutterclose\" [1]"); LXS.AppendLine( " \"float screenwindow\" ["+vScreenWindow[0]+" "+vScreenWindow[1]+" "+vScreenWindow[2]+" "+vScreenWindow[3]+"]"); LXS.AppendLine(""); LXS.AppendLine("Film \"fleximage\""); LXS.AppendLine(" \"integer xresolution\" [800]"); LXS.AppendLine(" \"integer yresolution\" [600]"); LXS.AppendLine(" \"integer haltspp\" [0]"); LXS.AppendLine(" \"bool premultiplyalpha\" [\"false\"]"); LXS.AppendLine(" \"string tonemapkernel\" [\"reinhard\"]"); LXS.AppendLine(" \"float reinhard_postscale\" [1.200000]"); LXS.AppendLine(" \"float reinhard_burn\" [6.000000]"); LXS.AppendLine(" \"integer displayinterval\" [8]"); LXS.AppendLine(" \"integer writeinterval\" [120]"); LXS.AppendLine(" \"string ldr_clamp_method\" [\"lum\"]"); LXS.AppendLine(" \"bool write_exr\" [\"false\"]"); LXS.AppendLine(" \"bool write_png\" [\"true\"]"); LXS.AppendLine(" \"string write_png_channels\" [\"RGB\"]"); LXS.AppendLine(" \"bool write_png_16bit\" [\"false\"]"); LXS.AppendLine(" \"bool write_png_gamutclamp\" [\"true\"]"); LXS.AppendLine(" \"bool write_tga\" [\"false\"]"); LXS.AppendLine(" \"string filename\" [\"C:\\\\temp\\\\lux\\\\"+ExportView.ViewName.ToString()+"\"]"); LXS.AppendLine(" \"bool write_resume_flm\" [\"false\"]"); LXS.AppendLine(" \"bool restart_resume_flm\" [\"true\"]"); LXS.AppendLine(" \"integer reject_warmup\" [128]"); LXS.AppendLine(" \"bool debug\" [\"false\"]"); LXS.AppendLine(" \"float colorspace_white\" [0.314275 0.329411]"); LXS.AppendLine(" \"float colorspace_red\" [0.630000 0.340000]"); LXS.AppendLine(" \"float colorspace_green\" [0.310000 0.595000]"); LXS.AppendLine(" \"float colorspace_blue\" [0.155000 0.070000]"); LXS.AppendLine(" \"float gamma\" [2.2]"); LXS.AppendLine(""); LXS.AppendLine("PixelFilter \"mitchell\""); LXS.AppendLine(" \"float B\" [0.75]"); LXS.AppendLine(" \"float C\" [0.125]"); LXS.AppendLine(""); LXS.AppendLine("Sampler \"metropolis\""); LXS.AppendLine(" \"float largemutationprob\" [0.4]"); LXS.AppendLine(""); LXS.AppendLine("SurfaceIntegrator \"bidirectional\""); LXS.AppendLine(" \"integer eyedepth\" [48]"); LXS.AppendLine(" \"integer lightdepth\" [48]"); LXS.AppendLine(""); LXS.AppendLine("VolumeIntegrator \"single\""); LXS.AppendLine(" \"float stepsize\" [1]"); LXS.AppendLine("Accelerator \"tabreckdtree\""); LXS.AppendLine(" \"integer intersectcost\" [80]"); LXS.AppendLine(" \"integer traversalcost\" [1]"); LXS.AppendLine(" \"float emptybonus\" [0.2]"); LXS.AppendLine(" \"integer maxprims\" [1]"); LXS.AppendLine(" \"integer maxdepth\" [-1]"); LXS.AppendLine(""); LXS.AppendLine("WorldBegin"); LXS.AppendLine(""); LXS.AppendLine("AttributeBegin"); LXS.AppendLine("LightGroup \"default\""); LXS.AppendLine(" LightSource \"sunsky\""); LXS.AppendLine("AttributeEnd"); LXS.AppendLine(""); LXS.AppendLine("Include \""+sOutputPathMatFile+"\""); LXS.AppendLine(""); foreach (String FilePath in GeoFileList) { LXS.AppendLine("Include \"" + FilePath + "\""); } LXS.AppendLine(""); LXS.AppendLine("WorldEnd"); //create outputfilepath String sOutputPathSceneFile = vOutputFilePath + ExportView.Name.ToString()+"/"+ ExportView.ViewName.ToString() + ".lxs"; //write out Scene file using (System.IO.StreamWriter LXSFile = new System.IO.StreamWriter(sOutputPathSceneFile)) { LXSFile.WriteLine(LXS); } } }