internal static void AssignValues( Schema schema, DataStorage dataStorage, string fieldPartsHosts, string fieldHostsAssemblies, string fieldPartsStrTypes, IDictionary <string, ISet <string> > partHostValues, IDictionary <string, ISet <string> > hostAssemblyValues, SortedList <string, SortedSet <string> > partsStrTypes) { // 5. Create an entity based on the schema Entity entity = new Entity(schema); // 6. Assign values to the fields for the entity entity.Set <IDictionary <string, string> >( fieldPartsHosts, ConvertToSimpleDic(partHostValues)); entity.Set <IDictionary <string, string> >( fieldHostsAssemblies, ConvertToSimpleDic(hostAssemblyValues)); entity.Set <IDictionary <string, string> >( fieldPartsStrTypes, ConvertToSimpleDic(partsStrTypes)); // 7. Associate the entity with a revit element using (Transaction t = new Transaction(dataStorage.Document)) { t.Start("Save Data"); dataStorage.SetEntity(entity); t.Commit(); } }
internal static bool AssignValues(Schema schema, DataStorage dataStorage, IDictionary <string, ISet <string> > values) { try { // 4. Create an entity based on the schema Entity entity = new Entity(schema.GUID); // 5. Assign values to the fields foreach (string key in values.Keys) { Field field = schema.GetField(key); if (field != null) { // only IList is supported IList <string> components = values[key].ToList(); entity.Set <IList <string> >(key, components); } } // 6. Associate the entity with a Revit element using (Transaction t = new Transaction(dataStorage.Document, "Set Entity")) { t.Start(); dataStorage.SetEntity(entity); t.Commit(); } return(true); } catch (Exception ex) { Autodesk.Revit.UI.TaskDialog.Show("Exception", $"{ex.Message}"); return(false); } }
/// <summary> /// Add spool number to element /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddButton_Click(object sender, RoutedEventArgs e) { System.Drawing.Color color = ColorSelected; this.Hide(); int c; bool isNumeric = int.TryParse(this.NumberBox.Text, out c); if (isNumeric) { if (tools.count < int.Parse(this.NumberBox.Text)) { tools.count = c; } try { using (Autodesk.Revit.DB.Transaction AssingPartNumberT = new Autodesk.Revit.DB.Transaction(tools.uidoc.Document, "Assing Part Number")) { AssingPartNumberT.Start(); tools.AddToSelection(); var partNumber = tools.createNumbering(this.PrefixBox.Text, this.SeparatorBox.Text, tools.count, this.NumberBox.Text.Length); //tools.AssingPartNumber(tools.selectedElement, partNumber); foreach (Autodesk.Revit.DB.Element x in tools.selectedElements) { tools.AssingPartNumber(x, partNumber); } tools.count += 1; //count 5, pad left int leadingZeros = NumberBox.Text.Length > 1 ? this.NumberBox.Text.Length - tools.count.ToString().Length : 0; this.NumberBox.Text = (new string('0', leadingZeros)) + tools.count.ToString(); tools.writeConfig(this.PrefixBox.Text, this.SeparatorBox.Text, this.NumberBox.Text); AssingPartNumberT.Commit(); } } catch (System.Exception ex) { //MessageBox.Show(ex.Message); } } else { SuffixContent suffixContentForm = new SuffixContent(); suffixContentForm.Topmost = true; suffixContentForm.WindowStartupLocation = WindowStartupLocation.CenterScreen; suffixContentForm.ShowDialog(); //MessageBox.Show("Number field should contain only numbers"); } this.ShowDialog(); }
private void DeleteTransaction(List <Autodesk.Revit.DB.ElementId> list) { using (Autodesk.Revit.DB.Transaction t = new Autodesk.Revit.DB.Transaction(doc, "Delete Imported DWGs")) { t.Start(); doc.Delete(list); t.Commit(); } }
void SetUpSizeAndPrint( Autodesk.Revit.DB.ViewSheet vs, Autodesk.Revit.DB.PrintManager printManager, Autodesk.Revit.DB.IPrintSetting printSetting ) { Autodesk.Revit.DB.BoundingBoxUV bbUV = vs.Outline; double x = UnitUtils.ConvertFromInternalUnits( bbUV.Max.U - bbUV.Min.U, Autodesk.Revit.DB.DisplayUnitType.DUT_MILLIMETERS); double y = UnitUtils.ConvertFromInternalUnits( bbUV.Max.V - bbUV.Min.V, Autodesk.Revit.DB.DisplayUnitType.DUT_MILLIMETERS); if (x == 0 || y == 0) { return; } string sheetSize = GetSheetSize(x, y, 100); if (x > y) { printSetting.PrintParameters.PageOrientation = Autodesk.Revit.DB.PageOrientationType.Landscape; } else { printSetting.PrintParameters.PageOrientation = Autodesk.Revit.DB.PageOrientationType.Portrait; } foreach (Autodesk.Revit.DB.PaperSize ps in printManager.PaperSizes) { if (ps.Name == sheetSize) { printSetting.PrintParameters.PaperSize = ps; break; } } Trace.Write("Selected paper sized: " + printSetting.PrintParameters.PaperSize.Name); using (Autodesk.Revit.DB.Transaction t = new Autodesk.Revit.DB.Transaction(vs.Document)) { t.Start("temp"); printManager.PrintSetup.CurrentPrintSetting = printSetting; //printManager.Apply(); printManager.PrintSetup.SaveAs("temp"); printManager.SubmitPrint(vs); //vs.Print(true); t.RollBack(); } }
/// <summary> /// Desplace all values down /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DiplaceDn_Click(object sender, RoutedEventArgs e) { this.Hide(); try { using (Autodesk.Revit.DB.Transaction DisplaceUp = new Autodesk.Revit.DB.Transaction(tools.uidoc.Document, "Displace Up Part Number")) { DisplaceUp.Start(); tools.SetElementsDnStream(); DisplaceUp.Commit(); } } catch { } this.ShowDialog(); }
internal static DataStorage GetDataStorage(Document doc, string name) { DataStorage dataStorage = new Autodesk.Revit.DB.FilteredElementCollector(doc) .OfClass(typeof(DataStorage)) .Where(ds => ds.Name == name) .FirstOrDefault() as DataStorage; if (dataStorage == null) { using (Transaction t = new Transaction(doc, "Create data storag")) { t.Start(); dataStorage = DataStorage.Create(doc); dataStorage.Name = name; t.Commit(); } } return(dataStorage); }
/// <summary> /// This method is called by PerformanceAdviser after all elements in document /// matching the ElementFilter from GetElementFilter() are checked by ExecuteElementCheck(). /// /// This method checks to see if there are any elements (door instances, in this case) in the /// m_FlippedDoor instance member. If there are, it iterates through that list and displays /// the instance name and door tag of each item. /// </summary> /// <param name="document">The active document</param> public void FinalizeCheck(Autodesk.Revit.DB.Document document) { if (m_FlippedDoors.Count == 0) { System.Diagnostics.Debug.WriteLine("No doors were flipped. Test passed."); } else { //Pass the element IDs of the flipped doors to the revit failure reporting APIs. Autodesk.Revit.DB.FailureMessage fm = new Autodesk.Revit.DB.FailureMessage(m_doorWarningId); fm.SetFailingElements(m_FlippedDoors); Autodesk.Revit.DB.Transaction failureReportingTransaction = new Autodesk.Revit.DB.Transaction(document, "Failure reporting transaction"); failureReportingTransaction.Start(); Autodesk.Revit.DB.PerformanceAdviser.GetPerformanceAdviser().PostWarning(fm); failureReportingTransaction.Commit(); m_FlippedDoors.Clear(); } }
public bool LoadFamily(string path) { Autodesk.Revit.DB.Family family = null; lock (_locker) { TaskContainer.Instance.EnqueueTask(uiapp => { try { var doc = uiapp.ActiveUIDocument.Document; using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc, "loadFamily")) { trans.Start(); var isLoad = doc.LoadFamily(path, out family); if (isLoad) { Debug.Print("{0}:{1}", "load is success", family.Name); } else { Debug.Print("load is failed"); } trans.Commit(); } } finally { lock (_locker) { Monitor.Pulse(_locker); } } }); Monitor.Wait(_locker); } return(family != null); }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">A message that can be set by the external application /// which will be displayed if a failure or cancellation is returned by /// the external command.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>Return the status of the external command. /// A result of Succeeded means that the API external method functioned as expected. /// Cancelled can be used to signify that the user cancelled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation.</returns> public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { Autodesk.Revit.DB.Transaction newTran = null; try { newTran = new Autodesk.Revit.DB.Transaction(commandData.Application.ActiveUIDocument.Document, "ViewPrinter"); newTran.Start(); PrintMgr pMgr = new PrintMgr(commandData); if (null == pMgr.InstalledPrinterNames) { PrintMgr.MyMessageBox("No installed printer, the external command can't work."); return(Autodesk.Revit.UI.Result.Cancelled); } using (PrintMgrForm pmDlg = new PrintMgrForm(pMgr)) { if (pmDlg.ShowDialog() != DialogResult.Cancel) { newTran.Commit(); return(Autodesk.Revit.UI.Result.Succeeded); } newTran.RollBack(); } } catch (Exception ex) { if (null != newTran) { newTran.RollBack(); } message = ex.ToString(); return(Autodesk.Revit.UI.Result.Failed); } return(Autodesk.Revit.UI.Result.Cancelled); }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">A message that can be set by the external application /// which will be displayed if a failure or cancellation is returned by /// the external command.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>Return the status of the external command. /// A result of Succeeded means that the API external method functioned as expected. /// Cancelled can be used to signify that the user cancelled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation.</returns> public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { Autodesk.Revit.DB.Transaction newTran = null; try { newTran = new Autodesk.Revit.DB.Transaction(commandData.Application.ActiveUIDocument.Document, "ViewPrinter"); newTran.Start(); PrintMgr pMgr = new PrintMgr(commandData); if (null == pMgr.InstalledPrinterNames) { PrintMgr.MyMessageBox("No installed printer, the external command can't work."); return Autodesk.Revit.UI.Result.Cancelled; } using (PrintMgrForm pmDlg = new PrintMgrForm(pMgr)) { if (pmDlg.ShowDialog() != DialogResult.Cancel) { newTran.Commit(); return Autodesk.Revit.UI.Result.Succeeded; } newTran.RollBack(); } } catch (Exception ex) { if (null != newTran) newTran.RollBack(); message = ex.ToString(); return Autodesk.Revit.UI.Result.Failed; } return Autodesk.Revit.UI.Result.Cancelled; }
void SetUpSizeAndPrint(Autodesk.Revit.DB.ViewSheet vs, Autodesk.Revit.DB.PrintManager printManager, Autodesk.Revit.DB.IPrintSetting printSetting) { Autodesk.Revit.DB.BoundingBoxUV bbUV = vs.Outline; double x = bbUV.Max.U - bbUV.Min.U; double y = bbUV.Max.V - bbUV.Min.V; if (x == 0 || y == 0) { return; } x = Converter.ConvertFromInternalUnits(x, Autodesk.Revit.DB.DisplayUnitType.DUT_MILLIMETERS); y = Converter.ConvertFromInternalUnits(y, Autodesk.Revit.DB.DisplayUnitType.DUT_MILLIMETERS); Trace.Write("x = " + x + "; y = " + y); string sheetSize = GetSheetSize(x, y, 100); if (x > y) { printSetting.PrintParameters.PageOrientation = Autodesk.Revit.DB.PageOrientationType.Landscape; } else { printSetting.PrintParameters.PageOrientation = Autodesk.Revit.DB.PageOrientationType.Portrait; } Trace.Write("sheetSize = " + sheetSize + "; PageOrientation: " + printSetting.PrintParameters.PageOrientation.ToString()); foreach (Autodesk.Revit.DB.PaperSize ps in printManager.PaperSizes) { if (ps.Name == sheetSize) { Trace.Write("ps.Name = " + ps.Name); printSetting.PrintParameters.PaperSize = ps; break; } } using (Autodesk.Revit.DB.Transaction t = new Autodesk.Revit.DB.Transaction(vs.Document)) { t.Start("temp"); printManager.PrintToFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + vs.Name + ".pdf"; printManager.PrintSetup.CurrentPrintSetting = printSetting; printManager.PrintSetup.SaveAs("temp"); printManager.Apply(); vs.Print(true); t.RollBack(); /*unsafe { * System.IntPtr hwnd = NativeDlls.FindWindow(null, "Сохранить PDF-файл как"); * Trace.Write("hwnd = " + hwnd.ToInt64()); * if (hwnd.ToInt64() > 0) { * long lResult = NativeDlls.SendMessage(hwnd, 16, 0, 0); * } * }*/ } }
public void MAGN_7075() { string filePath = Path.Combine(workingDirectory, @".\Bugs\MAGN_7075.dyn"); string testPath = Path.GetFullPath(filePath); //open the test file ViewModel.OpenCommand.Execute(testPath); AssertNoDummyNodes(); RunCurrentModel(); //Create 4 curve elements in Revit List <Autodesk.Revit.DB.Element> curves = new List <Autodesk.Revit.DB.Element>(); var document = DocumentManager.Instance.CurrentUIDocument.Document; Autodesk.Revit.DB.Plane plane; Autodesk.Revit.DB.SketchPlane sp; using (var trans = new Autodesk.Revit.DB.Transaction(document, "CreateModelCurvesInRevit")) { trans.Start(); Point[] points = { Point.ByCoordinates(0, 0, 0), Point.ByCoordinates(200, 0, 0), Point.ByCoordinates(200, 100, 0), Point.ByCoordinates(0, 100, 0) }; var line1 = Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(points[0], points[1]); var line2 = Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(points[1], points[2]); var line3 = Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(points[2], points[3]); var line4 = Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(points[3], points[0]); var revitLine1 = line1.ToRevitType(false); var revitLine2 = line2.ToRevitType(false); var revitLine3 = line3.ToRevitType(false); var revitLine4 = line4.ToRevitType(false); plane = Autodesk.Revit.DB.Plane.CreateByNormalAndOrigin(new Autodesk.Revit.DB.XYZ(0, 0, 1), new Autodesk.Revit.DB.XYZ(0, 0, 0)); sp = Autodesk.Revit.DB.SketchPlane.Create(document, plane); var modelCurv1 = document.Create.NewModelCurve(revitLine1, sp); var modelCurv2 = document.Create.NewModelCurve(revitLine2, sp); var modelCurv3 = document.Create.NewModelCurve(revitLine3, sp); var modelCurv4 = document.Create.NewModelCurve(revitLine4, sp); trans.Commit(); curves.Add(modelCurv1); curves.Add(modelCurv2); curves.Add(modelCurv3); curves.Add(modelCurv4); } var node = ViewModel.Model.CurrentWorkspace.Nodes.OfType <DSModelElementsSelection>().First(); node.UpdateSelection(curves); RunCurrentModel(); //Create a line in Revit Autodesk.Revit.DB.ElementId lineID; using (var trans = new Autodesk.Revit.DB.Transaction(document, "CreateModelLine")) { trans.Start(); var line = document.Create.NewModelCurve(Autodesk.Revit.DB.Line.CreateBound(new Autodesk.Revit.DB.XYZ(-100, 0, 0), new Autodesk.Revit.DB.XYZ(-50, 0, 0)), sp); lineID = line.Id; trans.Commit(); } RunCurrentModel(); //Delete the created line in Revit using (var trans = new Autodesk.Revit.DB.Transaction(document, "DeleteReferencePoint")) { trans.Start(); document.Delete(lineID); trans.Commit(); } RunCurrentModel(); //There should be no infinite loop, otherwise, there will be an error with this test case. }
/// <summary> /// This method is called by PerformanceAdviser after all elements in document /// matching the ElementFilter from GetElementFilter() are checked by ExecuteElementCheck(). /// /// This method checks to see if there are any elements (door instances, in this case) in the /// m_FlippedDoor instance member. If there are, it iterates through that list and displays /// the instance name and door tag of each item. /// </summary> /// <param name="document">The active document</param> public void FinalizeCheck(Autodesk.Revit.DB.Document document) { if (m_FlippedDoors.Count == 0) System.Diagnostics.Debug.WriteLine("No doors were flipped. Test passed."); else { //Pass the element IDs of the flipped doors to the revit failure reporting APIs. Autodesk.Revit.DB.FailureMessage fm = new Autodesk.Revit.DB.FailureMessage(m_doorWarningId); fm.SetFailingElements(m_FlippedDoors); Autodesk.Revit.DB.Transaction failureReportingTransaction = new Autodesk.Revit.DB.Transaction(document, "Failure reporting transaction"); failureReportingTransaction.Start(); document.PostFailure(fm); failureReportingTransaction.Commit(); m_FlippedDoors.Clear(); } }
public Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { System.Diagnostics.Trace.Listeners. Add(new System.Diagnostics.EventLogTraceListener("Application")); Autodesk.Revit.UI.UIDocument uidoc = commandData.Application.ActiveUIDocument; Autodesk.Revit.DB.Document doc = uidoc.Document; try { // set the location of a new .txt file string filePath = System.Environment .GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + doc.Title + ".txt"; // Create the .txt file containing shared parameter definitions using (System.IO.Stream s = System.IO.File.Create(filePath)) { s.Close(); } // set the file as a shared parameter source file to the application doc.Application.SharedParametersFilename = filePath; // Get access to the file Autodesk.Revit.DB.DefinitionFile defFile = doc.Application.OpenSharedParameterFile(); // Create a new group called 'ReadOnly' Autodesk.Revit.DB.DefinitionGroup defGroup = defFile.Groups.Create("ReadOnly"); // Create a new defintion Autodesk.Revit.DB.ExternalDefinitionCreationOptions defCrtOptns = new Autodesk.Revit.DB.ExternalDefinitionCreationOptions ("RM_BLOCK", Autodesk.Revit.DB.ParameterType.Text); defCrtOptns.UserModifiable = false; defCrtOptns.Visible = true; // Insert the definition into the group Autodesk.Revit.DB.Definition def = defGroup.Definitions.Create(defCrtOptns); // Lay out the categories to which the param // will be bound Autodesk.Revit.DB.CategorySet catSet = new Autodesk.Revit.DB.CategorySet(); catSet.Insert(doc.Settings.Categories .get_Item(Autodesk.Revit.DB.BuiltInCategory.OST_Rooms)); // Define a binding type Autodesk.Revit.DB.InstanceBinding instBnd = new Autodesk.Revit.DB.InstanceBinding(catSet); // Bind the parameter to the active document using (Autodesk.Revit.DB.Transaction t = new Autodesk.Revit.DB.Transaction(doc)) { t.Start("Add Param"); Autodesk.Revit.DB.SubTransaction st = new Autodesk.Revit.DB.SubTransaction(doc); doc.ParameterBindings.Insert (def, instBnd, Autodesk.Revit.DB.BuiltInParameterGroup.PG_DATA); t.Commit(); } Autodesk.Revit.DB.DefinitionBindingMapIterator itr = doc.ParameterBindings.ForwardIterator(); // while (itr.MoveNext()) { Autodesk.Revit.DB.InternalDefinition intDef = itr.Current as Autodesk.Revit.DB.InternalDefinition; } Autodesk.Revit.UI.TaskDialog.Show("Success", string.Format("The parameter called {0} has been successfully added to the document", def.Name)); return(Autodesk.Revit.UI.Result.Succeeded); } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Autodesk.Revit.UI.Result.Cancelled); } catch (System.Exception ex) { Autodesk.Revit.UI.TaskDialog.Show("Exception", string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); return(Autodesk.Revit.UI.Result.Failed); } }