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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值