Revit二次开发之“创建尺寸标注”

运行该命令,选中一个可标注的对象,即实现标注。
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class TestCommand : IExternalCommand
{
    
public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
    {
        UIDocument uiDoc 
= commandData.Application.ActiveUIDocument;
        Autodesk.Revit.ApplicationServices.Application app 
= commandData.Application.Application;
        Document doc 
= uiDoc.Document;
        Autodesk.Revit.DB.View view 
= doc.ActiveView;
        ViewType vt 
= view.ViewType;

        
if (vt == ViewType.FloorPlan || vt == ViewType.Elevation)
        {
            Reference eRef 
= uiDoc.Selection.PickObject(ObjectType.Element, "Please pick a curve based element like wall.");
            
if (eRef != null && eRef.Element != null)
            {
                XYZ dirVec 
= new XYZ();
                XYZ viewNormal 
= view.ViewDirection;

                LocationCurve locCurve 
= eRef.Element.Location as LocationCurve;
                
if (locCurve == null || locCurve.Curve == null)
                {
                    TaskDialog.Show(
"Prompt""Selected element isn’t curve based!");
                    
return Result.Cancelled;
                }

                
// location curve needs to be perpendicular to view normal
                XYZ dirCur = locCurve.Curve.get_EndPoint(0).Subtract(locCurve.Curve.get_EndPoint(1)).Normalize();
                
double d = dirCur.DotProduct(viewNormal);
                
if (d > -0.000000001 && d < 0.000000001)
                {
                    dirVec 
= dirCur.CrossProduct(viewNormal);
                    XYZ p1 
= locCurve.Curve.get_EndPoint(0);
                    XYZ p2 
= locCurve.Curve.get_EndPoint(1);
                    XYZ dirLine 
= XYZ.Zero.Add(p1);
                    XYZ newVec 
= XYZ.Zero.Add(dirVec);
                    newVec 
= newVec.Normalize().Multiply(3);
                    dirLine 
= dirLine.Subtract(p2);

                    p1 
= p1.Add(newVec);
                    p2 
= p2.Add(newVec);
                    
// move the dimension line a little away the element’s curve
                    Line newLine = app.Create.NewLine(p1, p2, true);

                    ReferenceArray arrRefs 
= new ReferenceArray();
                    Options options 
= app.Create.NewGeometryOptions();
                    options.ComputeReferences 
= true;
                    options.DetailLevel 
= DetailLevels.Fine;
                    GeometryElement element 
= eRef.Element.get_Geometry(options);
                    GeometryObjectArray geoObjectArray 
= element.Objects;
                    
//enum the geometry element
                    for (int j = 0; j < geoObjectArray.Size; j++)
                    {
                        GeometryObject geoObject 
= geoObjectArray.get_Item(j);
                        Solid solid 
= geoObject as Solid;
                        
if (solid == null)
                            
continue;

                        FaceArrayIterator fIt 
= solid.Faces.ForwardIterator();
                        
while (fIt.MoveNext())
                        {
                            PlanarFace p 
= fIt.Current as PlanarFace;
                            
if (p == null)
                                
continue;

                            p2 
= p.Normal.CrossProduct(dirLine);
                            
if (p2.IsZeroLength())
                            {
                                arrRefs.Append(p.Reference);
                            }
                            
if (2 == arrRefs.Size)
                            {
                                
break;
                            }
                        }
                        
if (2 == arrRefs.Size)
                        {
                            
break;
                        }
                    }
                    
if (arrRefs.Size != 2)
                    {
                        TaskDialog.Show(
"Prompt""Couldn’t find enough reference for creating dimension");
                        
return Result.Cancelled;
                    }

                    Transaction trans 
= new Transaction(doc, "create dimension");
                    trans.Start();
                    doc.Create.NewDimension(doc.ActiveView, newLine, arrRefs);
                    trans.Commit();
                }
                
else
                {
                    TaskDialog.Show(
"Prompt""Selected element isn’t curve based!");
                    
return Result.Cancelled;
                }
            }
        }
        
else
        {
            TaskDialog.Show(
"Prompt""Only support Plan View or Elevation View");
        }

        
return Result.Succeeded;
    }
}
from:http://revit.5d6d.com/thread-866-1-4.html
posted @ 2011-07-21 13:05  大气象  阅读(5149)  评论(2编辑  收藏  举报
http://www.tianqiweiqi.com