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(000);
            XYZ p2 
= new XYZ(1000);
            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手动
posted @ 2011-03-17 09:57  大气象  阅读(4291)  评论(1)    收藏  举报
http://www.tianqiweiqi.com