Macro - open document as detach from central and save the document.

Macro - open document as detach from central and save the document.

Anonymous
Not applicable
8,893 Views
28 Replies
Message 1 of 29

Macro - open document as detach from central and save the document.

Anonymous
Not applicable

Hello there,

 

I have come up with an idea of a Macro and would like to know whether it could be done and how could it be done. I have been working in a project and need to detach it from central for all the partners involved in the project, it is kind of an arduous task.

 

Therefore, I have thought on developing a macro with macro manager where i could open the active document as detach from central and they save it into a file.

 

Is this possible?

 

Many thanks!

0 Likes
8,894 Views
28 Replies
Replies (28)
Message 2 of 29

Anonymous
Not applicable

I don't know if you can instantly detach the current local file. 

But honestly you dont want to do that. worksharing enviroment is sometimes quite delicate and if you start detatching local files you might be sending the wrong files. .

 

I would create it as:

 

1. Sync to central & close the document

 

2. Open the central, detach and discard all the worksets

something like this should workish...

 

                    ModelPath MyCentralFile = doc.GetWorksharingCentralModelPath();

                    OpenOptions openoptions = new OpenOptions();
                    OpenOptions.DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets;

                    app.OpenAndActivateDocument(MyCentralFile, openoptions, false);

 

3. Save as a new file in folder x

4. Purge the drawing from all you dont need, save again

5. Done ?

 

Good luck!

 

0 Likes
Message 3 of 29

Anonymous
Not applicable

We actually are going through a few network pathing changes. (IT side) That are causing our files to have to be detached ->preserved ->resaved (same name) and relinquished all worksets. Do you think your script could be adapted to do this thorughout a directory?

 

 

0 Likes
Message 4 of 29

MehtaRajesh
Advocate
Advocate

Hi

Some kind of below script can Help you

m_app = commandData.Application;
app = m_app.Application;

// Check worksharing mode of each document
// Open Revit projects
OpenFileDialog theDialogRevit = new OpenFileDialog();
theDialogRevit.Title = "Select Revit Project Files";
theDialogRevit.Filter = "RVT files|*.rvt";
theDialogRevit.FilterIndex = 1;
theDialogRevit.InitialDirectory = @"D:\";
theDialogRevit.Multiselect = true;

if (theDialogRevit.ShowDialog() == DialogResult.OK)
{            
 string mpath = "";
        string mpathOnlyFilename = "";
        FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

        folderBrowserDialog1.Description = "Select Folder Where Revit Projects to be Saved in Local";
        folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;

        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK
        {
         mpath = folderBrowserDialog1.SelectedPath;
                foreach (String projectPath in theDialogRevit.FileNames)
                {
                 FileInfo filePath = new FileInfo(projectPath);
                        ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath.FullName);
                        OpenOptions opt = new OpenOptions();
                        opt.DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets;
                        mpathOnlyFilename = filePath.Name;
                        Document openedDoc = app.OpenDocumentFile(mp, opt);                              
                        SaveAsOptions options = new SaveAsOptions();
                        options.OverwriteExistingFile = true;
                        ModelPath modelPathout = ModelPathUtils.ConvertUserVisiblePathToModelPath(mpath + "\\" + mpathOnlyFilename);
                        openedDoc.SaveAs(modelPathout, options);
                        openedDoc.Close(false);
               }
        }
}

Regards,

Message 5 of 29

Anonymous
Not applicable

This wasn't written in c# was it?

0 Likes
Message 6 of 29

helengorina7713
Contributor
Contributor

Thanks for sharing the code.

 

there is a  minor typo in the second IF statement: Your code is missing the closing parenthesis - see in red

 

Environment.SpecialFolder.MyComputer;

        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
         mpath = folderBrowserDialog1.SelectedPath;

 

 

0 Likes
Message 7 of 29

MehtaRajesh
Advocate
Advocate

Hi,
This code is written in C#.
Thanks and Regards,

0 Likes
Message 8 of 29

erlich_denis
Observer
Observer

Hi!
I am trying to recreate the macro in Revit using your posted code, but have a problem with the m_app.Application as it is not defined.

for a record, there were System.IO ans System.Windows.Forms classes that were not defined in the Revit 2015 Macro SHrpDevelop.

 

Please, advise what is missing.

Thank you!

 

 

/*
 * Created by SharpDevelop.
 * User: user
 * Date: 1/9/2017
 * Time: 4:43 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

namespace test222
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")]
    public partial class ThisDocument
    {
        private void Module_Startup(object sender, EventArgs e)
        {

        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }
        
        public void exampleagain(){
            ThisDocument.a
            m_app = Document.Application;
app = m_app.Application;
// Check worksharing mode of each document
// Open Revit projects
OpenFileDialog theDialogRevit = new OpenFileDialog();
theDialogRevit.Title = "Select Revit Project Files";
theDialogRevit.Filter = "RVT files|*.rvt";
theDialogRevit.FilterIndex = 1;
theDialogRevit.InitialDirectory = @"D:\";
theDialogRevit.Multiselect = true;
if (theDialogRevit.ShowDialog() == DialogResult.OK)
{             
 string mpath = "";
        string mpathOnlyFilename = "";
        FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
        folderBrowserDialog1.Description = "Select Folder Where Revit Projects to be Saved in Local";
        folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
         mpath = folderBrowserDialog1.SelectedPath;
                foreach (String projectPath in theDialogRevit.FileNames)
                {
                 FileInfo filePath = new FileInfo(projectPath);
                        ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath.FullName);
                        OpenOptions opt = new OpenOptions();
                        opt.DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets;
                        mpathOnlyFilename = filePath.Name;
                        Document openedDoc = app.OpenDocumentFile(mp, opt);                               
                        SaveAsOptions options = new SaveAsOptions();
                        options.OverwriteExistingFile = true;
                        ModelPath modelPathout = ModelPathUtils.ConvertUserVisiblePathToModelPath(mpath + "\\" + mpathOnlyFilename);
                        openedDoc.SaveAs(modelPathout, options);
                        openedDoc.Close(false);
               }
        }
}
        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion
    }
}

0 Likes
Message 9 of 29

MehtaRajesh
Advocate
Advocate

Hi,

Add 

Autodesk.Revit.UI.UIApplication m_app;

Before

m_app = commandData.Application;

Check for following using and respected references

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

using Autodesk.Revit.ApplicationServices;

using System.Windows.Forms;

using Autodesk.Revit;

using System.IO;



Regards,
Rajesh

 

0 Likes
Message 10 of 29

erlich_denis
Observer
Observer
Thank you!
Someone actually suggested to do this:
Replacing these 2 lines

m_app = commandData.Application;
app = m_app.Application;

By this

Autodesk.Revit.ApplicationServices.Application app =this.Application;

And it worked as well.

Now the struggle is to silent all interruption/ such as missing links, double geometry etc. , that usual answere i " yes" and " ok" but i have to be physically present, so on a batch process of 68 central models it becomes a problem

With Regards,

Denis Erlich

BIM Manager Design-Build

Crosslinx Transit Solutions - Constructors


Tel.: 416-252-5315 Ext. 52258416-252-5315>

Mobile: 416-529-9856


4711 Yonge Street,

Toronto | Ontario | Canada | M2N 6K8416-252-5315>
0 Likes
Message 11 of 29

helengorina7713
Contributor
Contributor

here is the full solution- with some dialog for the user:

 

you need to add system references for the user dialog to work.

 

works in 2016, did not test in 2017

 

/*
 * Created by SharpDevelop.
 * User: gorina_admin
 * Date: 9/30/2016
 * Time: 2:09 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using System.IO;
using System.Windows.Forms;

namespace batch
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("30EBC375-5A4C-4917-AB07-D7212C9ED3FA")]
    public partial class ThisApplication
    {
        private void Module_Startup(object sender, EventArgs e)
        {

        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion
        public void savenotworkshared()
        {
            
            
OpenFileDialog theDialogRevit = new OpenFileDialog();
theDialogRevit.Title = "Select Revit Project Files";
theDialogRevit.Filter = "RVT files|*.rvt";
theDialogRevit.FilterIndex = 1;
theDialogRevit.InitialDirectory = @"C:\";
theDialogRevit.Multiselect = true;
if (theDialogRevit.ShowDialog() == DialogResult.OK)    
            
    {             
 string mpath = "";
        string mpathOnlyFilename = "";
        FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
        folderBrowserDialog1.Description = "Select Folder Where Revit Projects to be Saved in Local";
        folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
         mpath = folderBrowserDialog1.SelectedPath;
                foreach (String projectPath in theDialogRevit.FileNames)
                {
                 FileInfo filePath = new FileInfo(projectPath);
                        ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath.FullName);
                        OpenOptions opt = new OpenOptions();
                        opt.DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets;
                        mpathOnlyFilename = filePath.Name;
                        Document openedDoc = Application.OpenDocumentFile(mp, opt);                               
                        SaveAsOptions options = new SaveAsOptions();
                        options.OverwriteExistingFile = true;
                        ModelPath modelPathout = ModelPathUtils.ConvertUserVisiblePathToModelPath(mpath + "\\" + mpathOnlyFilename);
                        openedDoc.SaveAs(modelPathout, options);
                        openedDoc.Close(false);
               }
        }
}    

 

 

Helen Gorina

 

Perkins+Will    

Message 12 of 29

dnenov
Contributor
Contributor

Thanks so much for that! So useful 🙂

0 Likes
Message 13 of 29

sabeel.shaik
Contributor
Contributor

hey, can I get the final working macro for 2017. it will be very helpful.

thanks.

0 Likes
Message 14 of 29

helengorina7713
Contributor
Contributor
Have you tried it in 2017?
It should work in any version

0 Likes
Message 15 of 29

sabeel.shaik
Contributor
Contributor

its not working.

0 Likes
Message 16 of 29

helengorina7713
Contributor
Contributor
It works for me
Please send the file you are using it on
I will try to look at it next week
0 Likes
Message 17 of 29

sabeel.shaik
Contributor
Contributor

can u send me a private message. I will share details.

0 Likes
Message 18 of 29

helengorina7713
Contributor
Contributor

intercepting Revit messages is a herculean task where you need to know exactly what you are hunting for. Not feasible in a simple macro.  My targeted use was for multiple linked room models in a gigantic hospital. Those were typical rooms with beds and other equipment, and the team for some reason used worksharing even though for each room type there was just one planner. When you have huge amount of linked workshared file, Revit performance takes a hit. After making them not workshared, performance improved significantly

0 Likes
Message 19 of 29

helengorina7713
Contributor
Contributor

I have tested in 2019- everything is working fine.

do not forget about references - you need to configure them on your system
Image 12.png
and do not forget to re-build the solution on your machine
you need editing permissions to the folder where Revit stores Macros. Usually it is C:\ProgramData\Autodesk\Revit\Macros

Here is full code, and it works in any version of Revit from 2016 to 2019.1

 

/*
 * Created by SharpDevelop.
 * User: gorina_admin
 * Date: 9/30/2016
 * Time: 2:09 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 * 
 * rebuilt without errors for 2019.1 on 8/15/2018
 * 
 * 
 * 
 * 
 * 
 */
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using System.IO;
using System.Windows.Forms;

namespace batch
{
	[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
	[Autodesk.Revit.DB.Macros.AddInId("30EBC375-5A4C-4917-AB07-D7212C9ED3FA")]
	public partial class ThisApplication
	{
		private void Module_Startup(object sender, EventArgs e)
		{

		}

		private void Module_Shutdown(object sender, EventArgs e)
		{

		}

		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
		}
		#endregion
		public void savenotworkshared()
		{
			
			
			OpenFileDialog theDialogRevit = new OpenFileDialog();
			theDialogRevit.Title = "Select Revit Project Files";
			theDialogRevit.Filter = "RVT files|*.rvt";
			theDialogRevit.FilterIndex = 1;
			theDialogRevit.InitialDirectory = @"C:\";
			theDialogRevit.Multiselect = true;
			if (theDialogRevit.ShowDialog() == DialogResult.OK)
				
			{
				string mpath = "";
				string mpathOnlyFilename = "";
				FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
				folderBrowserDialog1.Description = "Select Folder Where Revit Projects to be Saved in Local";
				folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
				if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
				{
					mpath = folderBrowserDialog1.SelectedPath;
					foreach (String projectPath in theDialogRevit.FileNames)
					{
						FileInfo filePath = new FileInfo(projectPath);
						ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath.FullName);
						OpenOptions opt = new OpenOptions();
						opt.DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets;
						mpathOnlyFilename = filePath.Name;
						Document openedDoc = Application.OpenDocumentFile(mp, opt);
						SaveAsOptions options = new SaveAsOptions();
						options.OverwriteExistingFile = true;
						ModelPath modelPathout = ModelPathUtils.ConvertUserVisiblePathToModelPath(mpath + "\\" + mpathOnlyFilename);
						openedDoc.SaveAs(modelPathout, options);
						openedDoc.Close(false);
					}
				}
			}
		}
		
	}
}
Message 20 of 29

plamen.hristov
Participant
Participant

Hey Gorina,

I am new to Macros in Revit and I am trying your macro. Can you elaborate a little bit more on how to configure references or direct me to a source explaining those?

Thank you so much for your help!

0 Likes