AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79569069
Accepted
issamo
issamo
Asked: 2025-04-11 22:21:18 +0800 CST2025-04-11 22:21:18 +0800 CST 2025-04-11 22:21:18 +0800 CST

将身体面部投影到 Sketch 中

  • 772

亲爱的,我已经开始开发一个 VBA 代码,它应该:1- 要求用户从当前 CATPART 中的任何主体中选择一个面。2- 完成后,VBA 应该在几何集中提取该表面并在该表面的轮廓上创建 3 个点。3- 基于这些点,创建一个计划,最后将该表面轮廓投影到草图内。

在我的代码下面,它不起作用。它显示类型不匹配,但没有指定。

图片大致描述了我想要的内容。注意:catpart 可能包含复杂的形状,因此提取的曲面需要相切。

在此处输入图片描述

Sub ExtractSurfaceAndCreateSketch()
    Dim partDocument As Object
    Dim part1 As Object
    Dim selection1 As Object
    Dim face1 As Object
    Dim hybridShapeFactory As Object
    Dim hybridBody As Object
    Dim reference1 As Object
    Dim hybridShapeExtract As Object
    Dim hybridShapePoint As Object
    Dim sketch As Object
    Dim sketches As Object

    ' Initialize CATIA Document and Part
    Set partDocument = CATIA.activeDocument
    Set part1 = partDocument.part
    Set selection1 = partDocument.selection
    
    ' Clear previous selections
    selection1.Clear
    
    ' Prompt user to select a face
    MsgBox "Please select a face from a body."
    
    ' Select the face
    On Error Resume Next
    Dim result As Variant
    result = selection1.SelectElement2("Face", "Select a face", False)
    If Err.Number <> 0 Then
        MsgBox "Error during selection: " & Err.Description
        Err.Clear
        Exit Sub
    End If
    On Error GoTo 0
    
    ' Check the type of the result and compare accordingly
    If VarType(result) = vbString Then
        If result <> "Normal" Then
            MsgBox "No valid face selected. Exiting."
            Exit Sub
        End If
    Else
        MsgBox "Unexpected return type from SelectElement2. Exiting."
        Exit Sub
    End If
    
    ' Get the selected face
    If selection1.Count = 0 Then
        MsgBox "No face selected. Exiting."
        Exit Sub
    End If
    Set face1 = selection1.Item(1).Value
    
    ' Create a reference from the face
    On Error GoTo ErrorHandler
    Set reference1 = part1.CreateReferenceFromBRepName(face1.brepName)
    
    ' Create the Hybrid Shape Factory and Hybrid Body
    Set hybridShapeFactory = part1.hybridShapeFactory
    Set hybridBody = part1.hybridBodies.Add
    
    ' Extract the surface from the face
    Set hybridShapeExtract = hybridShapeFactory.AddNewExtract(reference1)
    hybridShapeExtract.Name = "ExtractedSurface"
    hybridBody.AppendHybridShape hybridShapeExtract
    
    ' Create points on the outline of the extracted surface
    Dim posX As Variant, posY As Variant
    Dim pointArray(2) As Object
    
    ' Define UV coordinates for point placement
    posX = Array(0.1, 0.5, 0.9) ' X coordinates
    posY = Array(0.1, 0.5, 0.9) ' Y coordinates
    
    Dim i As Integer
    For i = 0 To 2
        ' Create a point on the extracted surface using UV coordinates
        Set hybridShapePoint = hybridShapeFactory.AddNewPointOnSurface(reference1, posX(i), posY(i))
        hybridBody.AppendHybridShape hybridShapePoint
        Set pointArray(i) = hybridShapePoint ' Store the points for plane creation
    Next i
    
    ' Create a plane based on the three points
    Dim hybridShapePlane As Object
    Set hybridShapePlane = hybridShapeFactory.AddNewPlaneThroughPoints(pointArray(0), pointArray(1), pointArray(2))
    hybridBody.AppendHybridShape hybridShapePlane
    
    ' Create a sketch on the new plane
    Set sketches = part1.sketches
    Set sketch = sketches.Add(hybridShapePlane)
    
    ' Extract the projected profile of the extracted surface
    Dim projectedProfile As Object
    Set projectedProfile = hybridShapeFactory.AddNewProjection(hybridShapeExtract, hybridShapePlane)
    projectedProfile.Name = "ProjectedProfile"
    hybridBody.AppendHybridShape projectedProfile
    
    ' Set the sketch as editable
    Dim sketcherEditor As Object
    Set sketcherEditor = sketch.OpenEdition()
    
    ' Create lines based on the projected profile
    Dim iCurve As Integer
    Dim profile As Object

    ' Loop through all segments of the projected profile
    For iCurve = 1 To projectedProfile.Profiles.Count
        ' Get the profile curve
        Set profile = projectedProfile.Profiles.Item(iCurve)

        ' If it's a line, extract its start and end points
        If profile.Type = "Line" Then
            Dim startPoint As Object
            Dim endPoint As Object
            
            ' Retrieve the start and end points
            Set startPoint = profile.GetStartPoint()
            Set endPoint = profile.GetEndPoint()
            
            ' Create a line in the sketch using the Factory2D
            Dim factory2D As Object
            Set factory2D = sketcherEditor.factory2D
            
            ' Create the line in the sketch
            On Error Resume Next
            factory2D.CreateLine startPoint.X, startPoint.Y, endPoint.X, endPoint.Y
            If Err.Number <> 0 Then
                MsgBox "Error creating line: " & Err.Description
                Err.Clear
            End If
            On Error GoTo 0
        End If
    Next iCurve
    
    ' Close the sketch edition
    sketch.CloseEdition
    
    ' Update the part
    part1.Update
    
    MsgBox "Surface extracted, points created, plane established, and sketch projected successfully!"

    Exit Sub

ErrorHandler:
    MsgBox "Error: " & Err.Description
    Exit Sub
End Sub
vba
  • 1 1 个回答
  • 51 Views

1 个回答

  • Voted
  1. Best Answer
    Shrotter
    2025-04-12T13:59:02+08:002025-04-12T13:59:02+08:00

    您可以将面/提取部分直接投影到草图中,因此无需额外投影和创建线条。这是一个更简短的示例。我在草图中
    使用了其中一个。OriginElement

    Sub CATMain()
        Dim partDocument As PartDocument
        Dim part1 As Part
        Dim selection1 As Selection
        Dim hybridShapeFactory As Object
        Dim hybridBody As hybridBody 
        Dim reference1 As Reference
        Dim hybridShapeExtract As hybridShapeExtrac
        Dim sketch As Sketch
        Dim sketches As Sketches
        Dim InputObjectType(0)
        Dim Result as String
    
        ' Initialize CATIA Document and Part
        Set partDocument = CATIA.activeDocument
        Set part1 = partDocument.part
        Set selection1 = partDocument.selection
        
        ' Clear previous selections
        selection1.Clear
    
        ' Select the face
        InputObjectType(0)="PlanarFace" 
        result = selection1.SelectElement2(InputObjectType, "Select a face", False)
        
        ' Check the type of the result and compare accordingly
        If result <> "Normal"  Then
            MsgBox "No valid face selected. Exiting."
            Exit Sub
        End If
        
        ' Get the selected face
        If selection1.Count = 0 Then
            MsgBox "No face selected. Exiting."
            Exit Sub
        End If
    
        ' Create a reference from the face
        Set reference1 = selection1.Item(1).Reference
        
        ' Create the Hybrid Shape Factory and Hybrid Body
        Set hybridShapeFactory = part1.hybridShapeFactory
        Set hybridBody = part1.hybridBodies.Add
        
        ' Extract the surface from the face
        Set hybridShapeExtract = hybridShapeFactory.AddNewExtract(reference1)
        hybridShapeExtract.Name = "ExtractedSurface"
        hybridBody.AppendHybridShape hybridShapeExtract
        part1.Update
        
        ' Create a sketch on on YZ plane
        Set oRefPlane = part1.OriginElements.PlaneYZ
        Set sketches = hybridBody.HybridSketches
        Set sketch = sketches.Add(oRefPlane)
    
        ' Open the sketch
        Dim factory2D As Object
        Set factory2D = sketch.OpenEdition()
    
        'project extract in sketch
        Dim oProjection
        Set oProjection = factory2D.CreateProjection(hybridShapeExtract)
        
        ' Close the sketch edition
        sketch.CloseEdition
        
        ' Update the part
        part1.Update
        
        MsgBox "Surface extracted and sketch projected successfully!"
    
    End Sub
    
    • 0

相关问题

  • 搜索括号内的文本并设置其格式

  • 使用 VBA 将形状分配给 Powerpoint 中的组

  • Word VBA,将光标移动到编号列表中下一个数字的开头

  • 使用 vba 宏为每个帐户执行 Outlook 规则

  • 从访问 OLE 对象中提取文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve