void createExtention(Autodesk.Revit.DB.Document Revitdoc, Autodesk.Revit.ApplicationServices.Application RevitApp) { Document familyDoc = RevitApp.NewFamilyDocument(@"C:\ProgramData\Autodesk\RVT 2018\Family Templates\Chinese\公制常规模型.rft"); using (Transaction transaction = new Transaction(familyDoc)) { transaction.Start("Create family"); CurveArray curveArray = new CurveArray(); curveArray.Append(Line.CreateBound(new XYZ(0, 0, 0), new XYZ(5, 0, 0))); curveArray.Append(Line.CreateBound(new XYZ(5, 0, 0), new XYZ(5, 5, 0))); curveArray.Append(Line.CreateBound(new XYZ(5, 5, 0), new XYZ(5, 5, 0))); curveArray.Append(Line.CreateBound(new XYZ(0, 5, 0), new XYZ(5, 0, 0))); // CurveArrArray curveArrArray = new CurveArrArray(); // familyDoc.FamilyCreate.NewExtrusion(true, curveArrArray, SketchPlane.Create(familyDoc, RevitApp.Create.NewPointOnPlane(new XYZ(0, 0, 1), XYZ.Zero, 10))); } }
/// <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, ElementSet elements) { try { m_revit = commandData.Application.Application; m_familyDocument = commandData.Application.ActiveUIDocument.Document; // create new family document if active document is not a family document if (!m_familyDocument.IsFamilyDocument) { m_familyDocument = m_revit.NewFamilyDocument("Generic Model.rft"); if (null == m_familyDocument) { message = "Cannot open family document"; return(Autodesk.Revit.UI.Result.Failed); } } m_creationFamily = m_familyDocument.FamilyCreate; // create generic model family in the document CreateGenericModel(); if (0 == m_errCount) { return(Autodesk.Revit.UI.Result.Succeeded); } else { message = m_errorInfo; return(Autodesk.Revit.UI.Result.Failed); } } catch (Exception e) { message = e.ToString(); return(Autodesk.Revit.UI.Result.Failed); } }
/// <summary> /// Creates a negative block family from the geometry of the target element and boundaries. /// </summary> /// <remarks>This is the main implementation of the sample command.</remarks> /// <param name="targetElement">The target solid element.</param> /// <param name="boundaries">The selected curve element boundaries.</param> /// <param name="familyLoadOptions">The family load options when loading the new family.</param> /// <param name="familyTemplate">The family template.</param> public static FailureCondition CreateNegativeBlock(Element targetElement, IList <Reference> boundaries, IFamilyLoadOptions familyLoadOptions, String familyTemplate) { Document doc = targetElement.Document; Autodesk.Revit.ApplicationServices.Application app = doc.Application; // Get curve loop for boundary IList <Curve> curves = GetContiguousCurvesFromSelectedCurveElements(doc, boundaries); CurveLoop loop = null; try { loop = CurveLoop.Create(curves); } catch (Autodesk.Revit.Exceptions.ArgumentException) { // Curves are not contiguous return(FailureCondition.CurvesNotContigous); } List <CurveLoop> loops = new List <CurveLoop>(); loops.Add(loop); // Get elevation of loop double elevation = curves[0].GetEndPoint(0).Z; // Get height for extrusion BoundingBoxXYZ bbox = targetElement.get_BoundingBox(null); double height = bbox.Max.Z - elevation; if (height <= 1e-5) { return(FailureCondition.CurveLoopAboveTarget); } height += 1; // Create family Document familyDoc = app.NewFamilyDocument(familyTemplate); // Create block from boundaries Solid block = GeometryCreationUtilities.CreateExtrusionGeometry(loops, XYZ.BasisZ, height); // Subtract target element IList <Solid> fromElement = GetTargetSolids(targetElement); int solidCount = fromElement.Count; // Merge all found solids into single one Solid toSubtract = null; if (solidCount == 1) { toSubtract = fromElement[0]; } else if (solidCount > 1) { toSubtract = BooleanOperationsUtils.ExecuteBooleanOperation(fromElement[0], fromElement[1], BooleanOperationsType.Union); } if (solidCount > 2) { for (int i = 2; i < solidCount; i++) { toSubtract = BooleanOperationsUtils.ExecuteBooleanOperation(toSubtract, fromElement[i], BooleanOperationsType.Union); } } // Subtract merged solid from overall block try { BooleanOperationsUtils.ExecuteBooleanOperationModifyingOriginalSolid(block, toSubtract, BooleanOperationsType.Difference); } catch (Autodesk.Revit.Exceptions.InvalidOperationException) { return(FailureCondition.NoIntersection); } // Create freeform element using (Transaction t = new Transaction(familyDoc, "Add element")) { t.Start(); RevitFreeFormElement element = Autodesk.Revit.DB.FreeFormElement.Create(familyDoc, block); t.Commit(); } // Load family into document Family family = familyDoc.LoadFamily(doc, familyLoadOptions); familyDoc.Close(false); // Get symbol as first symbol of loaded family FilteredElementCollector collector = new FilteredElementCollector(doc); collector.WherePasses(new FamilySymbolFilter(family.Id)); FamilySymbol fs = collector.FirstElement() as FamilySymbol; // Place instance at location of original curves using (Transaction t2 = new Transaction(doc, "Place instance")) { t2.Start(); doc.Create.NewFamilyInstance(XYZ.Zero, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); t2.Commit(); } return(FailureCondition.Success); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; _doc = doc; //Prompt for Input contents Document fdocX = app.NewFamilyDocument("C:\\ProgramData\\Autodesk\\RVT 2013\\Family Templates\\English_I\\Annotations\\Generic Annotation.rft"); //Document fdocX = app.NewFamilyDocument("C:\\ProgramData\\Autodesk\\RVT 2013\\Family Templates\\English_I\\Generic Model.rft"); //New Qrencoder using the family document QREncoder qrcode = new QREncoder(fdocX, uidoc, app); string contents = Microsoft.VisualBasic.Interaction.InputBox("Enter and text to QR encode.", "QR Encode Prompt", "", -1, -1); if (contents != "") { if (null == fdocX) { return(Result.Failed); } // Modify document within a transaction using (Transaction tx = new Transaction(fdocX)) { tx.Start("Generate QR Code"); //GenerateQR(fdoc, qrcode, contents); GenerateQR(fdocX, qrcode, contents); tx.Commit(); } //save background family doc string dir = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); //Clean invalid filename characters string fileNameClean = CleanChars(contents); _familyName += fileNameClean; string filename = Path.Combine(dir, _familyName + ".rfa"); SaveAsOptions opt = new SaveAsOptions(); opt.OverwriteExistingFile = true; fdocX.SaveAs(filename, opt); fdocX.Close(false); //Insert new family into document using (Transaction tx2 = new Transaction(doc)) { tx2.Start("Insert QRCode"); Family fam = null; FilteredElementCollector fec = new FilteredElementCollector(doc) .OfClass(typeof(Family)); foreach (Family f in fec) { if (f.Name.Equals(_familyName)) { fam = f; } else { doc.LoadFamily(filename, out fam); } } FamilySymbol fs = null; foreach (FamilySymbol symbol in fam.Symbols) { fs = symbol; break; } XYZ p = uidoc.Selection.PickPoint("Please pick point to place QR code"); AnnotationSymbolTypeSet annoset = _doc.AnnotationSymbolTypes; AnnotationSymbolType annoSymbolType = null; foreach (AnnotationSymbolType type in annoset) { if (type.Name.Equals(_familyName)) { annoSymbolType = type; } } //Deprecated in 2013, but too lazy to implement new doc.Create.NewAnnotationSymbol(p, annoSymbolType, uidoc.ActiveView); //doc.Create.NewFamilyInstance(p, fs, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); tx2.Commit(); } return(Result.Succeeded); } else { return(Result.Cancelled); } }