明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3026|回复: 4

老大,为什么我以下的代码会提示”未找到主键"?

[复制链接]
发表于 2003-12-19 12:50:00 | 显示全部楼层 |阅读模式
为什么我以下的代码会提示”未找到主键"?也没有创建新层
环境:ACAD2004

Sub aaa()
   
    Dim a As AcadText
    Dim p As Variant
   
    p = ThisDrawing.Utility.GetPoint(, "pack a point")
    Set a = ThisDrawing.ModelSpace.AddText("新年好", p, 1)
    a.Layer = CreateLayer("123")
    ThisDrawing.Regen acActiveViewport
        
End Sub
Public Function CreateLayer(ssLayerName As String) As AcadLayer

    Set CreateLayer = ThisDrawing.Layers(ssLayerName)
    If Err Then
        Err.Clear
        Set CreateLayer = ThisDrawing.Layers.Add(ssLayerName)
    End If

End Function
发表于 2003-12-19 13:33:00 | 显示全部楼层
没有添加错误转向语句,怎么能够处理错误。

  1. Public Function CreateLayer(ssLayerName As String) As AcadLayer
  2.     [color=#DC143C]on error resume next[/color]
  3.     Set CreateLayer = ThisDrawing.Layers(ssLayerName)
  4.     If Err Then
  5.         Err.Clear
  6.         Set CreateLayer = ThisDrawing.Layers.Add(ssLayerName)
  7.     End If

  8. End Function
 楼主| 发表于 2003-12-20 09:24:00 | 显示全部楼层
我重写了更改图层的函数,问题得以解决!
Sub aaa()
   
    Dim a As AcadText
    Dim p As Variant
   
    p = ThisDrawing.Utility.GetPoint(, "pack a point")
    Set a = ThisDrawing.ModelSpace.AddText("新年好", p, 1)
    a.Layer = CreateLayer("456").Name
    ThisDrawing.Regen acActiveViewport
        
End Sub
Public Function CreateLayer(ssLayerName As String) As AcadLayer
   
    Dim i As Integer
    For i = 0 To ThisDrawing.Layers.Count - 1
        If ThisDrawing.Layers.Item(i).Name = ssLayerName Then
            CreateLayer = ThisDrawing.Layers(ssLayerName)
            Exit For
        Else
            Set CreateLayer = ThisDrawing.Layers.Add(ssLayerName)
            Exit For
        End If
        Next i

End Function
 楼主| 发表于 2003-12-20 09:28:00 | 显示全部楼层
明老大的函数更为简洁,佩服佩服.
------------------------------------------------
努力努力,再加把劲
发表于 2007-2-6 21:47:00 | 显示全部楼层
简单明了,我编的程序更复杂
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 20:45 , Processed in 0.180235 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表