NavisWorks Api 简单使用与Gantt

相信很多朋友在做BIM项目的时候.都有客户会提出项目计划,形象进度 等需求。

那么当前最主要的问题就是计划与BIM模型的关联问题.那么我在项目中是用户用Project软件编辑计划然后手动跟三维模型关联。如果同行有更好的解决办法请告诉我,非常感谢

1界面布局

2 代码部分

        //当前选中的模型的名称
        private string _currentSelectionName = string.Empty;
        //当前选中的模型的GUID
        private string _currentSelectionGuid = string.Empty;
        //当前选中的Item
        private GanttItem _currentGanttItem = null;、

 

       /// <summary>
        /// 选中计划 聚焦到对应的模型
        /// </summary>
        private void SetCurrentModel()
        {
            if (Autodesk.Navisworks.Api.Application.ActiveDocument != null)
            {
                if (_currentGanttItem != null)
                {
                    object modelid = _currentGanttItem.GetProperty("ModelId");
                    object modelName = _currentGanttItem.GetProperty("ModelName");

                    if (modelid != null)
                    {
                        if (!string.IsNullOrEmpty(modelid.ToString()))
                        {
                            Guid guid = Guid.Parse(modelid.ToString());
                            List<Autodesk.Navisworks.Api.ModelItem> seletedObjs = Autodesk.Navisworks.Api.Application.ActiveDocument.Models.RootItemDescendantsAndSelf.WhereInstanceGuid(guid).ToList();
                            if (seletedObjs.Any())
                            {
                                Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument;
                                oDoc.CurrentSelection.CopyFrom(seletedObjs);
                                oDoc.ActiveView.FocusOnCurrentSelection();
                            }

                        }

                    }
                }

            }
        }
 /// <summary>
        /// 将当前选中的模型 附件到计划  与计划匹配
        /// </summary>
        void MT_AddCurrentModel_Click(object sender, EventArgs e)
        {
            //验证模型
            if (Autodesk.Navisworks.Api.Application.ActiveDocument != null &&
               !Autodesk.Navisworks.Api.Application.ActiveDocument.IsClear)
            {
                Autodesk.Navisworks.Api.Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
                if (doc != null)
                {

                    if (doc.CurrentSelection.SelectedItems.Any())
                    {
                        //获取当前选中的模型信息
                        var selectItem = doc.CurrentSelection.SelectedItems[0];
                        if (selectItem != null)
                        {
                            _currentSelectionGuid = selectItem.InstanceGuid.ToString();
                            _currentSelectionName = selectItem.DisplayName;

                        }
                        //获取当前选中的Task
                        if (_currentGanttItem != null)
                        {
                            var exist = _taskList.Where(o => o.ModelId == _currentSelectionGuid);
                            if (exist.Any())
                            {
                                XtraMessageBox.Show("尊敬的用户您好!当前模型已经在计划中添加了!谢谢", "系统提示");
                            }
                            else
                            {
                                var modify = _taskList.FirstOrDefault(o => o.ProjectName == _currentGanttItem.Text);
                                if (modify != null)
                                {
                                    modify.ModelName = _currentSelectionName;
                                    modify.ModelItem = selectItem;
                                    modify.ModelId = _currentSelectionGuid;
                                    BindData();
                                }
                            }
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("尊敬的用户您好!当前没有选中模型!请先选中模型!", "系统提示");
                    }

                }
            }
        }

Gantt 属性存储是以XML的形式存储

[Serializable]
[XmlRoot("Properties")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable, ISerializable
{
    public SerializableDictionary()
    {
    }

    [EditorBrowsable(EditorBrowsableState.Advanced)]
    public SerializableDictionary(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }

    [EditorBrowsable(EditorBrowsableState.Advanced)]
    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
    }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        XmlSerializer xmlSerializer = typeof(TKey).CreateDefaultXmlSerializer();
        XmlSerializer xmlSerializer2 = typeof(TValue).CreateDefaultXmlSerializer();
        bool isEmptyElement = reader.IsEmptyElement;
        reader.Read();
        if (!isEmptyElement)
        {
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                reader.ReadStartElement("item");
                reader.ReadStartElement("key");
                TKey key = (TKey)xmlSerializer.Deserialize(reader);
                reader.ReadEndElement();
                reader.ReadStartElement("value");
                TValue value = (TValue)xmlSerializer2.Deserialize(reader);
                reader.ReadEndElement();
                base.Add(key, value);
                reader.ReadEndElement();
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }
    }

    public void WriteXml(XmlWriter writer)
    {
        XmlSerializer xmlSerializer = typeof(TKey).CreateDefaultXmlSerializer();
        XmlSerializer xmlSerializer2 = typeof(TValue).CreateDefaultXmlSerializer();
        foreach (TKey key in base.Keys)
        {
            writer.WriteStartElement("item");
            writer.WriteStartElement("key");
            xmlSerializer.Serialize(writer, key);
            writer.WriteEndElement();
            writer.WriteStartElement("value");
            TValue val = base[key];
            xmlSerializer2.Serialize(writer, val);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}
SerializableDictionary 类
public static T FromXmlString<T>(string xml)
{
    if (string.IsNullOrEmpty(xml))
    {
        return default(T);
    }
    using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
    {
        return XmlObjectHelpers.FromXmlStream<T>(stream);
    }
}
FromXmlString
public static T DeserializeFromXml<T>(this string xml)
{
    return XmlObjectHelpers.FromXmlString<T>(xml);
} 

我用的是NavisWorks SDK 2015. 当然每个版本都不一样。如果有错误的地方请网友指出!

posted @ 2015-08-21 14:52  在 水 一 方  阅读(2486)  评论(2编辑  收藏  举报