Revit二次开发错误提示集锦

根据异常提示,快速准确判断错误原因,找到错误位置,是开发的基本功。

一、事务(待定)
错误提示:
Autodesk.Revit.Exceptions.InvalidOperationException:A sub-transaction can only be active inside an open Transaction.
错误原因:
为了方便使用Add-In-Manager调试,把命令统一写成如下格式:
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public   class  HelloWorld : IExternalCommand
{
    
public  Result Execute(ExternalCommandData commandData,  ref   string  messages, ElementSet elements)
    {
        MessageBox.Show(
" Hello World " );
        
return  Result.Succeeded;
    }
}
但是在创建Revit对象比如Pipe的时候,这样的属性就会提示上面的错误,必须这样限制,使用手写Add-In文件。
[Transaction(TransactionMode.Automatic)]
[Regeneration(RegenerationOption.Manual)]
[Journaling(JournalingMode.NoCommandData)]
public   class  NewPipeCommand : IExternalCommand
{
    
public  Result Execute(ExternalCommandData commandData,  ref   string  messages, ElementSet elements)
    {
        UIApplication uiApp 
=  commandData.Application;
        Document doc 
=  uiApp.ActiveUIDocument.Document;
        CreateNewPipe(doc);
        
return  Result.Succeeded;
    }
    
public  Pipe CreateNewPipe(Document document)
    {
        FilteredElementCollector collector 
=   new  FilteredElementCollector(document);
        collector.OfClass(
typeof (PipeType));
        PipeType pipeType 
=  collector.FirstElement()  as  PipeType;

        Pipe pipe 
=   null ;
        
if  ( null   !=  pipeType)
        {
            
// create pipe between 2 points
            XYZ p1  =   new  XYZ( 0 0 0 );
            XYZ p2 
=   new  XYZ( 10 0 0 );
            pipe 
=  document.Create.NewPipe(p1, p2, pipeType);
        }
        
return  pipe;
    }
}
NewPipe.addin
<? xml version = " 1.0 "  encoding = " utf-8 " ?>
< RevitAddIns >
  
< AddIn Type = " Command " >
    
< Assembly > F:\RevitCodes\RevitCodes\bin\Debug\RevitCodes.dll </ Assembly >
    
< ClientId > 738dfa84 - e739 - 48c9 - 892c - 4a397f08b49f </ ClientId >
    
< FullClassName > RevitCodes.NewPipeCommand </ FullClassName >
    
< Text > NewPipe </ Text >
    
< Description > "" </ Description >
    
< VisibilityMode > AlwaysVisible </ VisibilityMode >
  
</ AddIn >
</ RevitAddIns >
这样便可以正常运行,创建Pipe
Automatic自动
Manual手动
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值