예제 #1
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Document      doc   = Util.GetActiveDocument(uidoc, false);

            if (null == doc)
            {
                return(Result.Failed);
            }

            Schema schema = CmdStore.GetExistingSchema();

            if (null == schema)
            {
                return(Result.Succeeded);
            }

            FilteredElementCollector a = GetAllElements(doc);

            // Map stored file data to list of descriptions
            // of the BIM elements that it is stored on:

            Dictionary <string, List <string> > map
                = new Dictionary <string, List <string> >(
                      new PathComparer());

            foreach (Element e in a)
            {
                Entity ent = e.GetEntity(schema);

                if (ent.IsValid())
                {
                    string filename = ent.Get <string>(schema.GetField("Filename"));
                    string folder   = ent.Get <string>(schema.GetField("Folder"));
                    string path     = Path.Combine(folder, filename);
                    if (!map.ContainsKey(path))
                    {
                        map.Add(path, new List <string>(1));
                    }
                    map[path].Add(Util.ElementDescription(e));
                }
            }

            if (0 == map.Count)
            {
                Util.InfoMessage("No EstoreFile data is "
                                 + "present in this Revit document.");

                return(Result.Succeeded);
            }

            List <string> keys = new List <string>(map.Keys);

            keys.Sort();

            string content   = string.Empty;
            int    nElements = 0;
            string elist;

            foreach (string path in keys)
            {
                nElements += map[path].Count;

                //ids = string.Join( ", ",
                //  map[path].ConvertAll<string>( id =>
                //    id.IntegerValue.ToString() ).ToArray() );

                //ids = string.Join( ", ",
                //  map[path].ToArray() );

                elist = string.Join("\r\n  ",
                                    map[path].ToArray());

                content += string.Format(
                    "\r\n{0}: \r\n  {1}", path, elist);
            }

            int n = keys.Count;

            string instruction = string.Format(
                "{0} file{1} stored on {2} element{3}:",
                n, Util.PluralSuffix(n), nElements,
                Util.PluralSuffix(nElements));

            Util.InfoMessage(instruction, content);

            return(Result.Succeeded);
        }
예제 #2
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Document      doc   = Util.GetActiveDocument(uidoc, true);

            if (null == doc)
            {
                return(Result.Failed);
            }

            Schema schema = CmdStore.GetExistingSchema();

            if (null == schema)
            {
                return(Result.Succeeded);
            }

            Selection sel = uidoc.Selection;

            IList <Reference> refs;

            //int n = sel.Elements.Size; // 2014
            int n = sel.GetElementIds().Count; // 2015

            if (0 < n)
            {
                refs = new List <Reference>(n);

                //foreach( Element e2 in sel.Elements ) // 2014
                foreach (ElementId id in sel.GetElementIds()) // 2015
                {
                    Element e = doc.GetElement(id);
                    refs.Add(new Reference(e));
                }
            }
            else
            {
                try
                {
                    refs = sel.PickObjects(
                        ObjectType.Element,
                        new EstoreFileFilter(schema),
                        "Please select elements from which to "
                        + "remove stored file data: ");
                }
                catch (RvtOperationCanceledException)
                {
                    return(Result.Cancelled);
                }
            }

            n = refs.Count;

            if (0 == n)
            {
                Util.InfoMessage("No elements selected.");

                return(Result.Succeeded);
            }

            string q = string.Format(
                "Remove the file data stored in extensible "
                + "storage on {0} selected element{1}?",
                n, Util.PluralSuffix(n));

            if (Util.Question(q))
            {
                Transaction t = new Transaction(doc);

                t.Start("Remove Stored Files");

                Element e = null;

                foreach (Reference r in refs)
                {
                    e = doc.GetElement(r.ElementId);

                    e.DeleteEntity(schema);
                }

                t.Commit();
            }
            return(Result.Succeeded);
        }
예제 #3
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Document      doc   = Util.GetActiveDocument(uidoc, false);

            if (null == doc)
            {
                return(Result.Failed);
            }

            Selection sel = uidoc.Selection;

            //int n = sel.Elements.Size; // 2014
            int n = sel.GetElementIds().Count; // 2015

            if (1 < n)
            {
                Util.InfoMessage(string.Format(
                                     "{0} element{1} selected. Please select one "
                                     + "single element to restore file data from.",
                                     n, Util.PluralSuffix(n)));

                return(Result.Failed);
            }

            Schema schema = CmdStore.GetExistingSchema();

            if (null == schema)
            {
                return(Result.Failed);
            }

            Element e = null;

            if (0 < n)
            {
                Debug.Assert(1 == n,
                             "we already checked for 1 < n above");

                //foreach( Element e2 in sel.Elements ) // 2014
                foreach (ElementId id in sel.GetElementIds()) // 2015
                {
                    e = doc.GetElement(id);
                }
            }
            else
            {
                try
                {
                    Reference r = sel.PickObject(
                        ObjectType.Element,
                        "Please pick an element to restore the file from: ");

                    e = doc.GetElement(r.ElementId);
                }
                catch (RvtOperationCanceledException)
                {
                    return(Result.Cancelled);
                }
            }

            Entity ent = e.GetEntity(schema);

            if (null == ent || !ent.IsValid())
            {
                Util.InfoMessage("No EstoreFile data "
                                 + "found on selected element.");

                return(Result.Failed);
            }

            string filename = ent.Get <string>(schema.GetField("Filename"));
            string folder   = ent.Get <string>(schema.GetField("Folder"));

            byte[] data = ent.Get <IList <byte> >(schema.GetField("Data")).ToArray <byte>();

            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Title        = "Restore File from Revit Extensible Storage";
            dlg.AddExtension = false;
            dlg.FileName     = filename;

            if (Directory.Exists(folder))
            {
                dlg.InitialDirectory = folder;
            }

            if (DialogResult.OK != dlg.ShowDialog())
            {
                return(Result.Cancelled);
            }

            File.WriteAllBytes(dlg.FileName, data);

            return(Result.Succeeded);
        }