Beispiel #1
0
        private void AddURL(string name, string link, string category, List <stringedLink> exLinks)
        {
            ComApi.InwOpState10 state = ComApiBridge.ComApiBridge.State;

            // create the hyperlink collection
            ComApi.InwURLOverride oMyURLOoverride = (ComApi.InwURLOverride)state.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwURLOverride, null, null);

            foreach (var ex in exLinks)
            {
                // create one hyperlink
                ComApi.InwURL2 oMyURL = (ComApi.InwURL2)state.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwURL, null, null);

                oMyURL.name = ex.Link_Name;
                oMyURL.URL  = ex.Link_URL;
                oMyURL.SetCategory(ex.Link_Category, "LcOaURL" + ex.Link_Category + "Hyperlink");

                // add the new hyperlink to the hyperlink collection
                ComApi.InwURLColl oURLColl = oMyURLOoverride.URLs();
                oURLColl.Add(oMyURL);

                // get current selected items
                ModelItemCollection modelItemCollectionIn = new ModelItemCollection(navisApp.ActiveDocument.CurrentSelection.SelectedItems);
                //convert to InwOpSelection of COM API
                ComApi.InwOpSelection comSelectionOut = ComApiBridge.ComApiBridge.ToInwOpSelection(modelItemCollectionIn);
                // set the hyplerlink of the model items
                state.SetOverrideURL(comSelectionOut, oMyURLOoverride);
                // enable to the hyperlinks visible
                state.URLsEnabled = true;
            }

            // create recent hyperlink
            {
                ComApi.InwURL2 oMyURL = (ComApi.InwURL2)state.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwURL, null, null);

                oMyURL.name = name;
                oMyURL.URL  = link;
                oMyURL.SetCategory(category, "LcOaURL" + category + "Hyperlink");

                // add the new hyperlink to the hyperlink collection
                ComApi.InwURLColl oURLColl = oMyURLOoverride.URLs();
                oURLColl.Add(oMyURL);

                // get current selected items
                ModelItemCollection modelItemCollectionIn = new ModelItemCollection(navisApp.ActiveDocument.CurrentSelection.SelectedItems);
                //convert to InwOpSelection of COM API
                ComApi.InwOpSelection comSelectionOut = ComApiBridge.ComApiBridge.ToInwOpSelection(modelItemCollectionIn);
                // set the hyplerlink of the model items
                state.SetOverrideURL(comSelectionOut, oMyURLOoverride);
                // enable to the hyperlinks visible
                state.URLsEnabled = true;
            }
        }
Beispiel #2
0
        private void AddURL(ModelItem modelItem, string[] urls, string[] titleUrls)
        {
            ComApi.InwOpState10 state;
            state = ComApiBridge.ComApiBridge.State;
            // create the hyperlink collection
            ComApi.InwURLOverride oMyURLOoverride = (ComApi.InwURLOverride)state.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwURLOverride, null, null);
            // get current selected items
            ModelItemCollection modelItemCollectionIn = new ModelItemCollection();

            modelItemCollectionIn.Add(modelItem);
            // add the new hyperlink to the hyperlink collection
            ComApi.InwURLColl oURLColl = oMyURLOoverride.URLs();

            for (int i = 0; i < urls.Count(); i++)
            {
                // create one hyperlink
                ComApi.InwURL2 oMyURL = (ComApi.InwURL2)state.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwURL, null, null);
                // create Hyperlink
                oMyURL.SetCategory("Hyperlink", "LcOaURLCategoryHyperlink");
                // Attachment Point of the hyperlink
                ComApi.InwLPos3f oNewP = (ComApi.InwLPos3f)state.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwLPos3f, null, null);
                oNewP.data1 = modelItemCollectionIn.BoundingBox().Center.X;
                oNewP.data2 = modelItemCollectionIn.BoundingBox().Center.Y;
                oNewP.data3 = modelItemCollectionIn.BoundingBox().Center.Z;

                oMyURL.AttachmentPoints().Add(oNewP);
                // name of the hyperlink
                oMyURL.name = titleUrls[i];
                // site of the hyperlink
                oMyURL.URL = urls[i];

                oURLColl.Add(oMyURL);
            }

            //convert to InwOpSelection of COM API
            ComApi.InwOpSelection comSelectionOut = ComApiBridge.ComApiBridge.ToInwOpSelection(modelItemCollectionIn);
            // set the hyplerlink of the model items
            state.SetOverrideURL(comSelectionOut, oMyURLOoverride);
            // enable to the hyperlinks visible
            state.URLsEnabled = true;

            modelItemCollectionIn.Remove(modelItem);
        }
Beispiel #3
0
        /// <summary>
        /// Замена и добавление ссылок только через COM - http://adndevblog.typepad.com/aec/2012/05/create-hyperlinks-for-model-objects-using-net-api.html
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public override int Execute(params string[] parameters)
        {
            try
            {
                ChangeLinksProps changeLinksPropsWindow = new ChangeLinksProps();
                bool?            result = changeLinksPropsWindow.ShowDialog();
                if (result != null && result.Value)
                {
                    Document oDoc = Application.ActiveDocument;

                    ModelItemEnumerableCollection allItems = oDoc.Models.RootItemDescendantsAndSelf;

                    ComApi.InwOpState10 state;
                    state = ComApiBridge.ComApiBridge.State;

                    foreach (ModelItem item in allItems)
                    {
                        DataProperty urlProp = item.PropertyCategories.FindPropertyByName("LcOaExURLAttribute", "LcOaURLAttributeURL");

                        if (urlProp != null)
                        {
                            ComApi.InwOaPath p_path = ComApiBridge.ComApiBridge.ToInwOaPath(item);
                            try
                            {
                                ComApi.InwURLOverride urlOverride = state.GetOverrideURL(p_path);
                                ComApi.InwURLColl     oURLColl    = urlOverride.URLs();
                                bool changed = false;//становится true если была поменяна хотябы 1 ссылка
                                foreach (ComApi.InwURL2 url in oURLColl)
                                {
                                    //Проверять исходный URL
                                    string initialUrl = url.URL;

                                    if (!String.IsNullOrEmpty(initialUrl))
                                    {
                                        string newUrl = null;
                                        if (changeLinksPropsWindow.ChangeAllUrls)
                                        {
                                            //Нужно заменить целиком весь путь до файла
                                            char slash = '\\';
                                            if (initialUrl.Contains("/"))
                                            {
                                                slash = '/';
                                            }
                                            List <string> temp = initialUrl.Split(slash).ToList();
                                            temp.RemoveAt(temp.Count - 1);
                                            string fileName = initialUrl.Split(slash).Last();
                                            newUrl = changeLinksPropsWindow.NewUrlFragment + slash + fileName;
                                        }
                                        else if (initialUrl.Contains(changeLinksPropsWindow.OldUrlFragment))
                                        {
                                            //Если путь содержит подстроку, введенную в окне, то нужно заменить эту подстроку
                                            newUrl = initialUrl.Replace(changeLinksPropsWindow.OldUrlFragment, changeLinksPropsWindow.NewUrlFragment);
                                        }

                                        if (newUrl != null)
                                        {
                                            url.URL = newUrl;
                                            changed = true;
                                        }



                                        /*
                                         * //Получение директории по-разному для локальных путей и для интернета
                                         * string initialDir = null;
                                         * char slash = '\\';
                                         * if (initialUrl.Contains("/"))
                                         * {
                                         *  slash = '/';
                                         *  Uri uri = new Uri(initialUrl);
                                         *  Uri initialDirUri = new Uri(uri, ".");
                                         *  initialDir = initialDirUri.ToString().TrimEnd('/');
                                         * }
                                         * else
                                         * {
                                         *  //Записанный путь может содержать недопустимые символы из-за которых вываливается ошибка в методе GetDirectoryName
                                         *  //initialDir = Path.GetDirectoryName(initialUrl);//выдает ошибку
                                         *  List<string> temp = initialUrl.Split('\\').ToList();
                                         *  temp.RemoveAt(temp.Count - 1);
                                         *  initialDir = String.Join("\\", temp.ToArray());
                                         * }
                                         *
                                         * string oldUrlToCompare = changeLinksPropsWindow.OldUrl;
                                         *
                                         *
                                         * //string fileName = Path.GetFileName(initialUrl);//выдает ошибку
                                         * string fileName = initialUrl.Split(slash).Last();
                                         *
                                         * if (changeLinksPropsWindow.ChangeAllUrls
                                         || oldUrlToCompare
                                         || //.StartsWith(initialDir)
                                         || .Equals(initialDir)
                                         || )
                                         ||{
                                         || //Разделитель может быть либо прямым либо обратным слешем
                                         || string divider = "/";
                                         || string newUrl = changeLinksPropsWindow.NewUrl;
                                         || if (newUrl.Contains("\\"))
                                         || {
                                         ||     divider = "\\";
                                         || }
                                         ||
                                         || url.URL = newUrl + divider + fileName;
                                         || changed = true;
                                         ||}
                                         */
                                    }
                                }
                                if (changed)
                                {
                                    ComApi.InwOpSelection comSelectionOut =
                                        ComApiBridge.ComApiBridge.ToInwOpSelection(new ModelItemCollection()
                                    {
                                        item
                                    });
                                    state.SetOverrideURL(comSelectionOut, urlOverride);
                                }
                            }
                            catch (System.Runtime.InteropServices.COMException)
                            { }
                        }
                    }

                    state.URLsEnabled = true;

                    Win.MessageBox.Show("Готово", "Готово", Win.MessageBoxButton.OK, Win.MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                CommonException(ex, "Ошибка при замене ссылок в Navis");
            }

            return(0);
        }