/*-------------------------------------------------------------------------
* 功能描述:FileSwitch
* 作者:xulisong
* 创建时间: 2019/5/28 10:31:29
* 版本号:v1.0
* -------------------------------------------------------------------------*/
using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FWindSoft.Revit
{
///
/// 文件状态
///
[Flags]
public enum DocumentState
{
///
/// 不可用
///
Invalid=0,
///
/// 操作前,已经打开
///
Exist=1,
///
/// 操作时,实时打开
///
New=2,
}
///
/// revit 文件开关
///
public class RevitFileSwitch
{
public RevitFileSwitch(string path)
{
Path = path;
State = DocumentState.Invalid;
}
///
/// 文件路径
///
public string Path { get; private set; }
///
/// 关联revit文件
///
public Document Document { get; private set; }
///
/// 文件状态
///
public DocumentState State { get; private set; }
///
/// 打开模型
///
public void On()
{
if (this.Document != null)
{
return;
}
string path = Path;
var sets = RevitCore.App.Documents;
if (Path == null || !File.Exists(path))
{
return;
}
foreach (Document document in sets)
{
if (document.PathName == path)
{
Document = document;
State = DocumentState.Exist;
break;
}
}
if (Document == null)
{
Document = RevitCore.App.OpenDocumentFile(path);
State = DocumentState.New;
}
}
///
/// 关闭模型
///
public void Off()
{
Document?.Close();
}
///
/// 关闭新状态的模型
///
public void OffNew()
{
if (DocumentState.New == State)
{
Document?.Close();
}
}
}
}