コード例 #1
0
        public Autodesk.Revit.UI.Result Execute(
            Autodesk.Revit.UI.ExternalCommandData commandData,
            ref string message, Autodesk.Revit.DB.ElementSet elementSet)
        {
            Autodesk.Revit.UI.UIApplication revit = commandData.Application;
            UIDocument  uidoc      = commandData.Application.ActiveUIDocument;
            Document    doc        = commandData.Application.ActiveUIDocument.Document;
            Transaction trans      = new Transaction(revit.ActiveUIDocument.Document, "Panel Tracker");
            ElementSet  collection = new ElementSet();

            Dictionary <string, List <String> > categories = new Dictionary <string, List <string> >();

            foreach (BuiltInCategory c in System.Enum.GetValues(typeof(BuiltInCategory)))
            {
                string cName = Category.GetCategory(doc, c).Name;
                categories.Add(cName, categoryparameters(doc, c));
            }

            string filePath = @"C:\Documents\Panel Tracker\tracker.txt";

            string xlFileLink    = "";
            string xlnamerow     = "";
            string xlnamecol     = "";
            string xlstatusrow   = "";
            string xlstatuscol   = "";
            string bimDocsLink   = "";
            bool   useParameter  = false;
            string parameterName = "";

            if (System.IO.File.Exists(filePath) && !(System.Windows.Forms.Control.ModifierKeys == Keys.N))
            {
                // Open the file to read from.
                using (System.IO.StreamReader sr = System.IO.File.OpenText(filePath))
                {
                    string        s  = "";
                    List <String> ls = new List <String>();
                    while ((s = sr.ReadLine()) != null)
                    {
                        ls.Add(s);
                    }

                    xlFileLink  = ls[0];
                    xlnamerow   = ls[1];
                    xlnamecol   = ls[2];
                    xlstatusrow = ls[3];
                    xlstatuscol = ls[4];
                    bimDocsLink = ls[5];

                    if (ls[6] == "1")
                    {
                        useParameter = true;
                    }
                    else
                    {
                        useParameter = false;
                    }
                    parameterName = ls[7];
                }
            }
            else
            {
                mLocalForms = new LocalForms();
                mLocalForms.CreateForm();

                //Example with direct call to sub menu.
                bool displayForm = true;
                while (displayForm)
                {
                    if (mLocalForms.trackerForm == null)
                    {
                        mLocalForms.trackerForm = new Tracker();
                    }
                    if (!formOpen)
                    {
                        mLocalForms.trackerForm.ShowDialog();
                        formOpen = true;
                    }
                    if (mLocalForms.trackerForm.exit)
                    {
                        displayForm = false;
                    }
                }
                if (mLocalForms.trackerForm.filled)
                {
                    bimDocsLink = mLocalForms.trackerForm.bimLink;
                    xlFileLink  = mLocalForms.trackerForm.sharepointLink;
                    xlnamerow   = mLocalForms.trackerForm.namerow;
                    xlnamecol   = mLocalForms.trackerForm.namecolumn;
                    xlstatusrow = mLocalForms.trackerForm.statusrow;
                    xlstatuscol = mLocalForms.trackerForm.statuscolumn;

                    useParameter  = mLocalForms.trackerForm.useParameter;
                    parameterName = mLocalForms.trackerForm.parameter;

                    // Create a file to write to.
                    using (System.IO.StreamWriter sw = System.IO.File.CreateText(filePath))
                    {
                        sw.WriteLine(xlFileLink);
                        sw.WriteLine(xlnamerow);
                        sw.WriteLine(xlnamecol);
                        sw.WriteLine(xlstatusrow);
                        sw.WriteLine(xlstatuscol);
                        sw.WriteLine(bimDocsLink);

                        if (useParameter == true)
                        {
                            sw.WriteLine("1");
                        }
                        else
                        {
                            sw.WriteLine("0");
                        }
                        sw.WriteLine(parameterName);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("UNFILLED");
                }
            }


            XLGuide guide = new XLGuide();

            guide.Info.Add("Panel Name", xlnamecol);
            guide.Info.Add("Status", xlstatuscol);

            try
            {
                guide.Row = Int32.Parse(xlnamerow);
            }
            catch (FormatException)
            {
                guide.Row = 0;
            }

            GetAssemblies(doc, uidoc, out List <Element> assemblies);

            assemParameters = new List <string>();

            foreach (Autodesk.Revit.DB.Parameter p in assemblies[0].Parameters)
            {
                assemParameters.Add(p.Definition.Name);
            }

            bool error = false;

            try
            {
                SharepointPull(xlFileLink, guide, out List <string> panFilter, out Dictionary <string, Panel> panList);

                //####################################################
                //#"Start" the transaction
                trans.Start();

                PanelUpdate(doc, uidoc, panFilter, panList, assemblies, useParameter, parameterName);

                //# "End" the transaction
                trans.Commit();
                //####################################################

                mLocalForms2 = new LocalForms2();
                mLocalForms2.CreateForm();
                mLocalForms2.oauthForm.ShowDialog();

                string fileName    = "";
                string oldFileName = doc.PathName;

                string docTitle = doc.Title;

                if (docTitle.Substring(doc.Title.Length - 4) == ".rvt")
                {
                    docTitle = docTitle.Substring(0, doc.Title.Length - 4);
                }
                if (docTitle.Contains("Panel_Tracker"))
                {
                    fileName = @"C:\Documents\Panel Tracker\" + docTitle + ".rvt";
                }
                else
                {
                    fileName = @"C:\Documents\Panel Tracker\" + docTitle + "Panel_Tracker.rvt";
                }
                SaveAsOptions options = new SaveAsOptions();
                options.OverwriteExistingFile = true;
                doc.SaveAs(fileName, options);

                doc.SaveAs(oldFileName, options);

                DocsUpload_(mLocalForms2.oauthForm.bearer, bimDocsLink, fileName);
            }
            catch (Exception e)
            {
                // if revit threw an exception, try to catch it
                TaskDialog.Show("Error", e.Message);
                error = true;
                return(Autodesk.Revit.UI.Result.Failed);
            }
            finally
            {
                // if revit threw an exception, display error and return failed
                if (error)
                {
                    TaskDialog.Show("Error", "Panel Tracker failed.");
                }
            }
            return(Autodesk.Revit.UI.Result.Succeeded);
        }
コード例 #2
0
        static Dictionary <string, Panel> SharepointPull(string xlFileLink, XLGuide guide, out List <string> panFilter, out Dictionary <string, Panel> panList)
        {
            panFilter = new List <string>();
            panList   = new Dictionary <string, Panel>();

            ClientContext ctx = new ClientContext("https://islandcompanies.sharepoint.com/sites/SiteDeliveries/");
            SecureString  pw  = new SecureString();

            foreach (char a in "Michael1994")
            {
                pw.AppendChar(a);
            }
            ctx.Credentials = new SharePointOnlineCredentials("*****@*****.**", pw);
            //ctx.Credentials = new NetworkCredential("*****@*****.**", "Michael1994", "islandcompanies");

            Web web = ctx.Web;

            ctx.Load(web, w => w.Title);

            getXLFileName(xlFileLink, out string xlFileName);
            getSharepointTree(xlFileLink, out List <String> sharepointTree);

            try
            {
                Folder sourceFolder = web.GetFolderByServerRelativeUrl(sharepointTree.Aggregate((i, j) => i + "/" + j));
                ctx.Load(ctx.Web);
                ctx.Load(sourceFolder);
                ctx.ExecuteQuery();

                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='Recursive'>
                                        <Query>
                                            <Where>
                                                <Eq>
                                                    <FieldRef Name='FileLeafRef'></FieldRef>
                                                    <Value Type='Text'>" + xlFileName + @"</Value>
                                                </Eq>
                                            </Where>
                                        </Query>
                                    </View>";
                //string srvRel = "sites/SiteDeliveries/Shared%20Documents/Packlists/01-000-000%20Panel%20Tracker/";
                ListItemCollection listItems = ctx.Web.Lists.GetByTitle("Documents").GetItems(camlQuery);
                ctx.Load(listItems);
                ctx.ExecuteQuery();

                Microsoft.SharePoint.Client.File file = listItems[0].File;
                ctx.Load(file);
                ctx.ExecuteQuery();

                FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
                ctx.ExecuteQuery();

                string filePath = @"C:\Documents\Panel Tracker\" + file.Name;
                using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                {
                    fileInfo.Stream.CopyTo(fileStream);
                }

                SpreadsheetDocument document = SpreadsheetDocument.Open(filePath, true);
                DocumentFormat.OpenXml.Spreadsheet.Workbook workbook = document.WorkbookPart.Workbook;
                string       sheetName  = "Panel Tracker";
                WorkbookPart workbookpt = document.WorkbookPart;
                Sheet        sheet      = workbook.Descendants <Sheet>().Where(S => S.Name == sheetName).FirstOrDefault();

                WorksheetPart wsPart = (WorksheetPart)(workbookpt.GetPartById(sheet.Id));

                bool run  = true;
                int  iter = 0;
                while (run == true)
                {
                    Panel p = new Panel();

                    bool   hasVal  = false;
                    bool   record  = false;
                    string panName = "";
                    foreach (KeyValuePair <string, string> entry in guide.Info)
                    {
                        try
                        {
                            Cell cell = new Cell();
                            cell   = GetCell(document, sheetName, entry.Value.ToString() + guide.Row.ToString(), out cell);
                            hasVal = true;
                            string v = cell.CellValue.InnerText;
                            if (entry.Key == "Panel Name")
                            {
                                p.Parameters.Add(entry.Key, v);
                                panName = v;
                            }
                            else
                            {
                                record = true;
                                string searchval = v.Split(')').Last();
                                p.Parameters.Add(entry.Key, searchval);
                            }
                        }
                        catch //(Exception ex)
                        {
                            p.Parameters.Add(entry.Key, null);
                            //Console.WriteLine(ex.Message);
                        }

                        iter++;
                    }
                    if (!hasVal)
                    {
                        run = false;
                    }
                    else
                    {
                        if (record)
                        {
                            panFilter.Add(panName);
                        }
                        panList.Add(panName, p);
                    }
                    guide.Row++;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
            //Console.ReadKey();

            return(panList);
        }