Пример #1
0
        private void InitializeCommand()
        {
            CommandBinding cbSelectParam = new CommandBinding(cmdSelectParam, (sender, e) =>
            {
                if ((e.OriginalSource as CheckBox).IsChecked == true)
                {
                    ParamListFiltered.Add(e.Parameter.ToString());
                }
                else
                {
                    ParamListFiltered.Remove(e.Parameter.ToString());
                }
            }, (sender, e) => { e.CanExecute = true; e.Handled = true; });

            CommandBinding cbParamsRefresh = new CommandBinding(cmdParamsRefresh, (sender, e) =>
            {
                ProcessSelection();

                CurrentElementList.ForEach(ele =>
                {
                    ParameterSet parameters = ele.Parameters;
                    foreach (Parameter parameter in parameters)
                    {
                        ParamListSource.Add(parameter.Definition.Name);
                    }
                });

                HashSet <string> hs = new HashSet <string>(ParamListSource);
                ParamListSource.Clear();
                hs.ToList().ForEach(d => ParamListSource.Add(d));
            }, (sender, e) => { e.CanExecute = true; e.Handled = true; });

            CommandBinding cbApplySelection = new CommandBinding(cmdApplySelection, (sender, e) =>
            {
                ProcessSelection();

                using (Transaction trans = new Transaction(doc, "CreateSelectionFilter"))
                {
                    trans.Start();
                    FilteredElementCollector collector   = new FilteredElementCollector(doc);
                    ICollection <Element> typecollection = collector.OfClass(typeof(SelectionFilterElement)).ToElements();
                    SelectionFilterElement selectset     = typecollection.Cast <SelectionFilterElement>().FirstOrDefault(ele => ele.Name == txtSelectFilterName.Text);
                    if (selectset != null)
                    {
                        selectset.Clear();
                    }
                    else
                    {
                        selectset = SelectionFilterElement.Create(doc, txtSelectFilterName.Text);
                    }

                    CurrentElementList.ForEach(ele => selectset.AddSingle(ele.Id));
                    //doc.ActiveView.IsolateElementsTemporary(selectset.GetElementIds());
                    trans.Commit();
                }
            }, (sender, e) => { e.CanExecute = true; e.Handled = true; });

            bnApplySelection.Command = cmdApplySelection;
            bnParamsRefresh.Command  = cmdParamsRefresh;

            ProcSF.CommandBindings.AddRange(new CommandBinding[]
            {
                cbSelectParam,
                cbParamsRefresh,
                cbApplySelection
            });
        }
        //
        public void selectByParaSpecial(UIDocument uiDoc)
        {
            // pick first element to get all parameters
            Document doc = uiDoc.Document;

            Reference myRef = uiDoc.Selection.PickObject(ObjectType.Element, "Select an Element...");

            Element myFirstElem = doc.GetElement(myRef);

            // Get parameters of element
            ParameterSet myParaSet = myFirstElem.Parameters;



            List <string> myListParaName        = new List <string>();
            List <string> myListParaValueString = new List <string>();

            bool saveSelection = false;

            foreach (Parameter myPara in myParaSet)
            {
                //TaskDialog.Show("abc", myPara.Definition.Name + "Value: " + myPara.AsValueString());
                myListParaName.Add(myPara.Definition.Name);

                if (myPara.StorageType == StorageType.String)
                {
                    myListParaValueString.Add(myPara.AsString());
                }

                else
                {
                    myListParaValueString.Add(myPara.AsValueString());
                }
            }

            //TaskDialog.Show("abc","num value: " + myListParaName.Count.ToString());
            //Reference myRef2 = uiDoc.Selection.PickObject(ObjectType.Element, new FilterByNameElementType(myListType));

            List <ElementId> myListIdElem = new List <ElementId>();

            string paraNameSelected  = "a";
            string paraValueSelected = "b";

            using (FilterForm myFormSelect = new FilterForm(myListParaName, myListParaValueString))

            {
                // Add list parameter to cb
                myFormSelect.ShowDialog();

                //if the user hits cancel just drop out of macro
                if (myFormSelect.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    //else do all this :)
                    myFormSelect.Close();
                    return;
                }

                if (myFormSelect.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    paraNameSelected  = myFormSelect.paraName_Cb.Text;
                    paraValueSelected = myFormSelect.value_Tb.Text;
                    saveSelection     = myFormSelect.saveSelection;
                    myFormSelect.Close();
                }
            }

            // Select by Filter

            List <Reference> myListRef = uiDoc.Selection.PickObjects(ObjectType.Element,
                                                                     new FilterByParameterValueSelectClass(paraNameSelected, paraValueSelected),
                                                                     "Select Element") as List <Reference>;

            foreach (Reference myRefe in myListRef) // Iterate through list of selected elements
            {
                // Add the ElementId of each selected element to the selection filter
                myListIdElem.Add(myRefe.ElementId);
            }

            if (saveSelection)
            {
                using (Transaction t = new Transaction(doc, "Create & Add To Selection Filter"))
                {
                    t.Start(); // Start the transaction

                    // Create a SelectionFilterElement
                    SelectionFilterElement selFilter = SelectionFilterElement.Create(doc, paraNameSelected + " - " + paraValueSelected);



                    foreach (Reference r in myListRef) // Iterate through list of selected elements
                    {
                        // Add the ElementId of each selected element to the selection filter
                        selFilter.AddSingle(r.ElementId);
                    }

                    // Commit the transaction
                    t.Commit();
                }
            }
            uiDoc.Selection.SetElementIds(myListIdElem);

            //TaskDialog.Show("abc", "number element: " + myListRef.Count);
        }
Пример #3
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            #region//Utils
            //String Builder
            StringBuilder sb = new StringBuilder();

            //Collector
            Collector collector = new Collector();

            //UnitConvertor
            LengthUnitConverter converter = new LengthUnitConverter();

            // Application context.
            var uidoc = commandData.Application.ActiveUIDocument;
            var doc   = uidoc.Document;
            #endregion

            #region//Check if we are in the Revit project , not in family one.
            if (doc.IsFamilyDocument)
            {
                Message.Display("Can't use command in family document", WindowType.Warning);
                return(Result.Cancelled);
            }
            #endregion

            #region//Get access to current view.
            var sectionView = uidoc.ActiveView;
            #endregion

            #region//Check if Dimension can be created in currently active project view.
            bool canCreateDimensionInView = false;
            switch (sectionView.ViewType)
            {
            case ViewType.FloorPlan:
                canCreateDimensionInView = false;
                break;

            case ViewType.CeilingPlan:
                canCreateDimensionInView = false;
                break;

            case ViewType.Detail:
                canCreateDimensionInView = false;
                break;

            case ViewType.Elevation:
                canCreateDimensionInView = true;
                break;

            case ViewType.Section:
                canCreateDimensionInView = true;
                break;
            }
            #endregion

            #region//Check if Dimension can be created
            if (!canCreateDimensionInView)
            {
                Message.Display("Dimension can't be created in the current view.", WindowType.Warning);
                return(Result.Cancelled);
            }
            #endregion

            #region//Check if activeView is a proper view
            if (canCreateDimensionInView)
            {
                #region//Ask user to select one generic model.
                Reference selectionReference;
                try
                {
                    selectionReference = uidoc.Selection.PickObject(ObjectType.Element, new SelectionFilterByCategory("Generic Models"), "Select one generic model.");
                }
                catch (Autodesk.Revit.Exceptions.OperationCanceledException)
                {
                    return(Result.Cancelled);
                }
                #endregion

                #region//Get main window
                Element        mainWindow       = doc.GetElement(selectionReference);
                FamilyInstance mainWindowFamily = doc.GetElement(mainWindow.Id) as FamilyInstance;
                FamilySymbol   mainWindowSymbol = mainWindowFamily.Symbol;

                #region//Get directions for dimensions
                XYZ widthDirection  = sectionView.RightDirection.Normalize();
                XYZ heigthDirection = new XYZ(0, 0, 1);
                #endregion

                #region// Checks if selection isn't empty
                if (mainWindowFamily == null)
                {
                    Message.Display("You haven't selected a valid element.\nPlease selected another element.", WindowType.Error);
                    return(Result.Cancelled);
                }
                #endregion

                #region//Check if generic model is in same direction as view
                XYZ genericModelDir = GetReferenceDirection(mainWindowFamily.GetReferences(FamilyInstanceReferenceType.CenterFrontBack).First(), doc);

                if (genericModelDir.ToString() != widthDirection.ToString())
                {
                    Message.Display("The generic model isn't parallel to the active view.", WindowType.Error);
                    return(Result.Cancelled);
                }
                #endregion

                #region//Get Type parameters
                double MDK_offset_vooraanzicht = mainWindowSymbol.LookupParameter("MDK_offset_vooraanzicht").AsDouble();
                double MDK_hoogte  = mainWindowSymbol.LookupParameter("MDK_hoogte").AsDouble();
                double MDK_breedte = mainWindowSymbol.LookupParameter("MDK_breedte").AsDouble();
                string MDK_merk    = mainWindowSymbol.LookupParameter("MDK_merk").AsString();
                #endregion

                #region//Get locationpoint of selected element
                LocationPoint mainWindowLocation      = mainWindowFamily.Location as LocationPoint;
                XYZ           mainWindowLocationpoint = mainWindowLocation.Point;
                #endregion

                #region//main window references
                ReferenceArray mainWindowHeight1 = new ReferenceArray();
                ReferenceArray mainWindowHeight2 = new ReferenceArray();
                ReferenceArray mainWindowWidth1  = new ReferenceArray();
                ReferenceArray mainWindowWidth2  = new ReferenceArray();

                foreach (var e in mainWindowFamily.GetReferences(FamilyInstanceReferenceType.Top))
                {
                    mainWindowHeight1.Append(e);
                    mainWindowHeight2.Append(e);
                }

                foreach (var e in mainWindowFamily.GetReferences(FamilyInstanceReferenceType.Bottom))
                {
                    mainWindowHeight1.Append(e);
                    mainWindowHeight2.Append(e);
                }

                foreach (var e in mainWindowFamily.GetReferences(FamilyInstanceReferenceType.Left))
                {
                    mainWindowWidth1.Append(e);
                    mainWindowWidth2.Append(e);
                }

                foreach (var e in mainWindowFamily.GetReferences(FamilyInstanceReferenceType.Right))
                {
                    mainWindowWidth1.Append(e);
                    mainWindowWidth2.Append(e);
                }

                foreach (Reference reference in mainWindowFamily.GetReferences(FamilyInstanceReferenceType.WeakReference))
                {
                    string name = mainWindowFamily.GetReferenceName(reference);
                    if (name.Contains("center_tussenregel"))
                    {
                        mainWindowHeight1.Append(reference);
                    }

                    if (name.Contains("center_tussenstijl"))
                    {
                        mainWindowWidth1.Append(reference);
                    }
                }
                #endregion
                #endregion

                #region//Get DimensionType
                DimensionType windowDimension           = collector.GetLinearDimensionTypeByName(doc, "FAC - Diagonal - 2mm - Black");
                DimensionType windowFrameDimension      = collector.GetLinearDimensionTypeByName(doc, "FAC - Diagonal - 2mm - Stelkozijn");
                DimensionType doorHandleDimension       = collector.GetLinearDimensionTypeByName(doc, "FAC - Ordinate - 2mm - Krukhoogte");
                DimensionType windowFrameLevelDimension = collector.GetLinearDimensionTypeByName(doc, "FAC - Ordinate - 2mm - Stelkozijn");
                #endregion

                #region//Get base line
                List <DetailLine> detailLines = collector.GetDetailLines(doc, sectionView.Id);
                string            lineStyle   = "FAC_vloerpeil";
                Line detailLine = null;

                foreach (DetailLine dl in detailLines)
                {
                    if (dl.LineStyle.Name == lineStyle)
                    {
                        detailLine = dl.GeometryCurve as Line;
                    }
                    else
                    {
                        Message.Display("Can't find a DetailLine with the LineStyle, FAC_vloerpeil.", WindowType.Error);
                        return(Result.Cancelled);
                    }
                }
                #endregion

                #region//Get windowframe and window family!!!MDK WORDT NOG VERVANGEN DOOR FAC KAN ERROR VEROORZAKEN
                ICollection <ElementId> subComponentIds = mainWindowFamily.GetSubComponentIds();

                #region//Get windowframe family
                string         windowFrameName = "31_MDK_GM_stelkozijn_lijn";
                FamilyInstance windowFrame     = null;

                foreach (ElementId id in subComponentIds)
                {
                    if (doc.GetElement(id).Name == windowFrameName)
                    {
                        windowFrame = doc.GetElement(id) as FamilyInstance;
                    }
                }

                #region//windowframe references
                ReferenceArray windowframeWidth   = new ReferenceArray();
                ReferenceArray windowframeHeight1 = new ReferenceArray();
                ReferenceArray windowframeHeight2 = new ReferenceArray();

                foreach (var e in windowFrame.GetReferences(FamilyInstanceReferenceType.Left))
                {
                    windowframeWidth.Append(e);
                }

                foreach (var e in windowFrame.GetReferences(FamilyInstanceReferenceType.Right))
                {
                    windowframeWidth.Append(e);
                }

                windowframeHeight2.Append(detailLine.Reference);

                foreach (var e in windowFrame.GetReferences(FamilyInstanceReferenceType.Top))
                {
                    windowframeHeight1.Append(e);
                    windowframeHeight2.Append(e);
                }

                foreach (var e in windowFrame.GetReferences(FamilyInstanceReferenceType.Bottom))
                {
                    windowframeHeight1.Append(e);
                    windowframeHeight2.Append(e);
                }
                #endregion
                #endregion

                #region//Get window family
                string                windowName       = "31_FAC_GM_vak_vleugel";
                FamilyInstance        window           = null;
                List <FamilyInstance> nestedFamilyList = new List <FamilyInstance>();

                foreach (ElementId id in subComponentIds)
                {
                    FamilyInstance fInstance = doc.GetElement(id) as FamilyInstance;
                    FamilySymbol   fSymbol   = fInstance.Symbol;

                    if (fSymbol.FamilyName == windowName)
                    {
                        window = doc.GetElement(id) as FamilyInstance;
                    }
                }

                ReferenceArray doorHandleHeightLevel = new ReferenceArray();
                ReferenceArray doorHandleHeight      = new ReferenceArray();

                XYZ windowLocationPoint   = null;
                XYZ windowDimensionPoint1 = null;

                Line windowDimension1 = null;

                if (window != null)
                {
                    #region//Get locationpoint of window
                    LocationPoint windowLocation = window.Location as LocationPoint;
                    windowLocationPoint = windowLocation.Point;
                    #endregion

                    #region//Create endpoint for line creation
                    windowDimensionPoint1 = GetDistance(windowLocationPoint, widthDirection, 0, -50);
                    #endregion

                    #region//Create line for dimension
                    windowDimension1 = Line.CreateBound(windowDimensionPoint1, windowDimensionPoint1 + heigthDirection * 100);
                    #endregion

                    #region//window references
                    doorHandleHeightLevel.Append(detailLine.Reference);

                    foreach (Reference reference in window.GetReferences(FamilyInstanceReferenceType.Bottom))
                    {
                        doorHandleHeight.Append(reference);
                    }

                    foreach (Reference reference in window.GetReferences(FamilyInstanceReferenceType.WeakReference))
                    {
                        string name = window.GetReferenceName(reference);
                        if (name.Contains("krukhoogte_binnen"))
                        {
                            doorHandleHeight.Append(reference);
                            doorHandleHeightLevel.Append(reference);
                        }
                    }
                    #endregion
                }
                #endregion
                #endregion

                #region//Create endpoints for line creation
                XYZ RightDimensionPoint1 = GetDistance(mainWindowLocationpoint, widthDirection, MDK_breedte, 150);
                XYZ RightDimensionPoint2 = GetDistance(mainWindowLocationpoint, widthDirection, MDK_breedte, 300);
                XYZ RightDimensionPoint3 = GetDistance(mainWindowLocationpoint, widthDirection, MDK_breedte, 450);

                XYZ BottomDimensionPoint1 = new XYZ(mainWindowLocationpoint.X, mainWindowLocationpoint.Y, mainWindowLocationpoint.Z + MDK_offset_vooraanzicht - converter.ConvertToFeet(150));
                XYZ BottomDimensionPoint2 = new XYZ(mainWindowLocationpoint.X, mainWindowLocationpoint.Y, mainWindowLocationpoint.Z + MDK_offset_vooraanzicht - converter.ConvertToFeet(300));

                XYZ LeftDimensionPoint1 = GetDistance(mainWindowLocationpoint, widthDirection, 0, -150);
                XYZ LeftDimensionPoint2 = GetDistance(mainWindowLocationpoint, widthDirection, 0, -300);

                XYZ TopDimensionPoint1 = new XYZ(mainWindowLocationpoint.X, mainWindowLocationpoint.Y, mainWindowLocationpoint.Z + MDK_offset_vooraanzicht + MDK_hoogte + converter.ConvertToFeet(150));
                #endregion

                #region//Create line for dimension
                Line RightDimension1 = Line.CreateBound(RightDimensionPoint1, RightDimensionPoint1 + heigthDirection * 100);
                Line RightDimension2 = Line.CreateBound(RightDimensionPoint2, RightDimensionPoint2 + heigthDirection * 100);
                Line RightDimension3 = Line.CreateBound(RightDimensionPoint3, RightDimensionPoint3 + heigthDirection * 100);

                Line BottomDimension1 = Line.CreateBound(BottomDimensionPoint1, BottomDimensionPoint1 + widthDirection * 100);
                Line BottomDimension2 = Line.CreateBound(BottomDimensionPoint2, BottomDimensionPoint2 + widthDirection * 100);

                Line LeftDimension1 = Line.CreateBound(LeftDimensionPoint1, LeftDimensionPoint1 + heigthDirection * 100);
                Line LeftDimension2 = Line.CreateBound(LeftDimensionPoint2, LeftDimensionPoint2 + heigthDirection * 100);

                Line TopDimension1 = Line.CreateBound(TopDimensionPoint1, TopDimensionPoint1 + widthDirection * 100);
                #endregion

                #region//Get selection filter
                SelectionFilterElement        filter  = null;
                List <SelectionFilterElement> filters = collector.GetSelectionFilter(doc);
                string filtername = "Stelkozijn maatvoering";

                foreach (SelectionFilterElement f in filters)
                {
                    if (f.Name == filtername)
                    {
                        filter = f;
                    }
                }
                #endregion

                #region//Create Annotations
                using (Transaction tx = new Transaction(doc))
                {
                    tx.Start("Create Annotations");

                    #region//Create and set workplane to place dimensions on
                    Plane       plane       = Plane.CreateByNormalAndOrigin(sectionView.ViewDirection, sectionView.Origin);
                    SketchPlane sketchPlane = SketchPlane.Create(doc, plane);
                    sectionView.SketchPlane = sketchPlane;
                    #endregion

                    #region//Create Dimensions
                    doc.Create.NewDimension(sectionView, RightDimension1, mainWindowHeight1, windowDimension);
                    doc.Create.NewDimension(sectionView, BottomDimension1, mainWindowWidth1, windowDimension);

                    Dimension windowFrameHeight1 = doc.Create.NewDimension(sectionView, LeftDimension1, windowframeHeight1, windowFrameDimension);
                    Dimension windowFrameHeight2 = doc.Create.NewDimension(sectionView, LeftDimension2, windowframeHeight2, windowFrameLevelDimension);

                    Dimension windowFrameWidth = doc.Create.NewDimension(sectionView, TopDimension1, windowframeWidth, windowFrameDimension);

                    #region//Add dimension to selection filter
                    filter.AddSingle(windowFrameWidth.Id);
                    filter.AddSingle(windowFrameHeight1.Id);
                    filter.AddSingle(windowFrameHeight2.Id);
                    #endregion

                    #region//Add prefix to window frame dimension
                    foreach (DimensionSegment seg in windowFrameHeight2.Segments)
                    {
                        seg.Prefix = "vlp +";
                    }
                    #endregion

                    #region//Add height dimension 2 and/or 3
                    if (mainWindowHeight1.Size != 2)
                    {
                        doc.Create.NewDimension(sectionView, RightDimension2, mainWindowHeight2, windowDimension);
                        if (window != null)
                        {
                            Dimension doorHandleHeight1 = doc.Create.NewDimension(sectionView, RightDimension3, doorHandleHeightLevel, doorHandleDimension);
                            Dimension doorHandleHeight2 = doc.Create.NewDimension(sectionView, windowDimension1, doorHandleHeight, doorHandleDimension);

                            foreach (DimensionSegment seg in doorHandleHeight1.Segments)
                            {
                                seg.Prefix = "GH =";
                                seg.Suffix = "+ vlp";
                            }

                            foreach (DimensionSegment seg in doorHandleHeight2.Segments)
                            {
                                seg.Prefix = "GH =";
                            }
                        }
                    }
                    else
                    {
                        if (window != null)
                        {
                            Dimension doorHandleHeight1 = doc.Create.NewDimension(sectionView, RightDimension2, doorHandleHeightLevel, doorHandleDimension);
                            Dimension doorHandleHeight2 = doc.Create.NewDimension(sectionView, windowDimension1, doorHandleHeight, doorHandleDimension);

                            foreach (DimensionSegment seg in doorHandleHeight1.Segments)
                            {
                                seg.Prefix = "GH =";
                                seg.Suffix = "+ vlp";
                            }

                            foreach (DimensionSegment seg in doorHandleHeight2.Segments)
                            {
                                seg.Prefix = "GH =";
                            }
                        }
                    }
                    #endregion

                    #region//Add width dimension 2
                    if (mainWindowWidth1.Size != 2)
                    {
                        doc.Create.NewDimension(sectionView, BottomDimension2, mainWindowWidth2, windowDimension);
                    }
                    #endregion

                    #endregion
                    tx.Commit();
                }
                #endregion
            }
            #endregion

            return(Result.Succeeded);
        }