Refine project overview to include specific business context (Blaidy Company) and the main workbook file. Restructure the architecture section into a layered object-oriented design (Data, Processing, Application layers) and define the responsibilities of core VBA classes. Add new technical documentation sections covering: - Model number format parsing structure - Condition expression language syntax - Category hierarchy logic and picking strategies - Material matching pipeline flow Include VBA code examples for key methods such as CollectAllMaterials and GetMaterialsByCategoryAndModel to illustrate recursive traversal and material retrieval logic. Clarify the structure of key Excel configuration sheets.
9.5 KiB
9.5 KiB
型号解析与物料匹配系统 - 使用说明
📋 目录
系统概述
功能简介
本系统实现了从产品型号中自动提取物料选择条件,并根据这些条件从BOM库中筛选出符合要求的物料清单。
核心功能
- ✅ 型号解析: 自动解析产品型号的各个组成部分
- ✅ 条件提取: 按规则提取物料选择条件(过程连接、接液材质、量程范围等)
- ✅ 条件匹配: 支持复杂的逻辑表达式(AND/OR/NOT/括号)
- ✅ 物料筛选: 自动筛选符合条件的物料
- ✅ 批量处理: 支持批量处理多个型号
- ✅ 可扩展性: 便于添加新的提取条件和匹配规则
示例
输入型号: YTHN-100.A0.532.M203.M16.Y3|BP-095.2312.M16.PA3
提取条件:
gclj(过程连接) =M20jycz(接液材质) =3lcfw(量程范围) =M16
匹配结果: 01011019018 - 高压接头部件
安装部署
1. 导入类模块
将以下4个类模块文件导入到VBA项目中:
- clsModelParser.cls - 型号解析器
- clsConditionExtractor.cls - 条件提取器
- clsConditionMatcher.cls - 条件匹配器
- clsBOMManager.cls - BOM管理器(扩展版本)
2. 导入标准模块
将以下2个标准模块导入到VBA项目中:
- modModelParserTest.bas - 测试模块
- modModelParserExamples.bas - 应用示例模块
3. 验证安装
运行测试过程验证安装是否成功:
' 在立即窗口或通过运行按钮执行
RunAllTests
如果所有测试通过,说明安装成功!
快速开始
最简单的使用方式
Sub QuickStart()
' 1. 创建BOM管理器并加载数据
Dim bomMgr As New clsBOMManager
bomMgr.LoadData ThisWorkbook.Worksheets("领料配置"), _
ThisWorkbook.Worksheets("平台配置清单")
' 2. 根据型号获取物料
Dim modelStr As String
modelStr = "YTHN-100.A0.532.M203.M16.Y3"
Dim materials As Collection
Set materials = bomMgr.GetMaterialsByModel(modelStr, "部件")
' 3. 查看结果
Dim mat As clsMaterialItem
For Each mat In materials
Debug.Print mat.Code & " - " & mat.Name
Next mat
End Sub
生成领料清单
运行以下示例生成完整的领料清单:
Example1_GeneratePickingListByModel
这将创建一个新工作表,包含完整的领料清单。
详细使用
1. 型号解析
基础解析
Dim parser As New clsModelParser
Dim modelStr As String
modelStr = "YTHN-100.A0.532.M203.M16.Y3|BP-095.2312.M16.PA3"
If parser.ParseModel(modelStr) Then
' 解析成功
Debug.Print "型号: " & parser.ModelType ' YTHN
Debug.Print "外径: " & parser.Diameter ' 100
Debug.Print "过程连接: " & parser.ConnectionCode ' M203
Debug.Print "量程: " & parser.RangeCode ' M16
Else
' 解析失败
Debug.Print "错误: " & parser.ErrorMessage
End If
获取提取的代码
Debug.Print "螺纹代码: " & parser.GetThreadCode() ' M20
Debug.Print "材质代码: " & parser.GetMaterialCode() ' 3
Debug.Print "量程代码: " & parser.GetRangeCode() ' M16
2. 条件提取
Dim parser As New clsModelParser
parser.ParseModel "YTHN-100.A0.532.M203.M16.Y3"
Dim extractor As New clsConditionExtractor
Dim conditions As Object
Set conditions = extractor.ExtractConditions(parser)
' 获取单个条件
Debug.Print "过程连接: " & extractor.GetConditionValue("gclj") ' M20
Debug.Print "接液材质: " & extractor.GetConditionValue("jycz") ' 3
Debug.Print "量程范围: " & extractor.GetConditionValue("lcfw") ' M16
' 遍历所有条件
Dim key As Variant
For Each key In conditions.Keys
Debug.Print key & " = " & conditions(key)
Next key
3. 条件匹配
简单条件
Dim matcher As New clsConditionMatcher
Dim conditions As Object
Set conditions = CreateObject("Scripting.Dictionary")
conditions("gclj") = "M20"
conditions("lcfw") = "M16"
' 等于
Debug.Print matcher.IsMatch("lcfw=M16", conditions) ' True
' 不等于
Debug.Print matcher.IsMatch("gclj!=M10", conditions) ' True
复合条件
' AND 运算
Debug.Print matcher.IsMatch("lcfw=M16 AND gclj=M20", conditions) ' True
' OR 运算
Debug.Print matcher.IsMatch("lcfw=M16 OR lcfw=M17", conditions) ' True
' 括号优先级
Debug.Print matcher.IsMatch("gclj=M20 AND (lcfw=M16 OR lcfw=M17)", conditions) ' True
4. 获取物料清单
指定类别
Dim bomMgr As New clsBOMManager
bomMgr.LoadData wsConfig, wsPlatform
' 获取"部件"类别的符合条件的物料
Dim materials As Collection
Set materials = bomMgr.GetMaterialsByModel("YTHN-100.A0.532.M203.M16.Y3", "部件")
所有类别
' 获取所有类别的符合条件的物料
Set materials = bomMgr.GetMaterialsByModel("YTHN-100.A0.532.M203.M16.Y3")
使用父类别/子类别逻辑
' 使用父类别物料(默认)
Set materials = bomMgr.GetMaterialsByCategoryAndModel( _
"YTHN-100.A0.532.M203.M16.Y3", "部件", True)
' 使用子类别物料(库存不足时)
Set materials = bomMgr.GetMaterialsByCategoryAndModel( _
"YTHN-100.A0.532.M203.M16.Y3", "部件", False)
API参考
clsModelParser
| 方法/属性 | 说明 | 返回类型 |
|---|---|---|
ParseModel(modelStr) |
解析型号字符串 | Boolean |
GetThreadCode() |
获取螺纹代码 | String |
GetMaterialCode() |
获取材质代码 | String |
GetRangeCode() |
获取量程代码 | String |
IsValid |
解析是否成功 | Boolean |
ErrorMessage |
错误信息 | String |
ToString() |
返回解析结果(调试用) | String |
clsConditionExtractor
| 方法/属性 | 说明 | 返回类型 |
|---|---|---|
ExtractConditions(parser) |
提取所有条件 | Dictionary |
GetConditionValue(varName) |
获取单个条件值 | String |
AddCondition(varName, value) |
手动添加条件 | - |
AddExtractionRule(...) |
添加提取规则 | - |
ToString() |
返回条件字符串(调试用) | String |
clsConditionMatcher
| 方法 | 说明 | 返回类型 |
|---|---|---|
IsMatch(expr, conditions) |
判断条件是否匹配 | Boolean |
TestExpression(expr) |
测试表达式有效性 | String |
clsBOMManager (新增方法)
| 方法 | 说明 | 返回类型 |
|---|---|---|
GetMaterialsByModel(modelStr, [categoryName]) |
根据型号获取物料 | Collection |
GetMaterialsByCategoryAndModel(...) |
根据类别和型号获取物料 | Collection |
ParseModelAndExtractConditions(modelStr) |
解析型号并返回条件 | Dictionary |
扩展指南
添加新的提取条件
假设需要添加"表盘类型"条件:
' 在 clsConditionExtractor.InitializeRules 方法中添加:
m_ExtractionRules("bplx") = Array("DialCode", "Direct")
添加新的提取方法
如果需要特殊的提取逻辑:
' 在 clsConditionExtractor.ExtractValue 方法中添加:
Case "GetMiddleChars"
' 提取中间几位字符
If Len(sourceValue) > 4 Then
ExtractValue = Mid(sourceValue, 2, 3)
Else
ExtractValue = sourceValue
End If
扩展条件匹配运算符
在 clsConditionMatcher.EvaluateSimpleCondition 中添加新运算符:
' 示例: 添加 > 运算符
ElseIf InStr(cond, ">") > 0 Then
' ... 实现大于比较
常见问题
Q1: 型号解析失败怎么办?
A: 检查型号格式是否正确:
- 表头部分必须包含:
型号-外径.安装.壳体.连接.量程.[特性] - 各部分用
.分隔 - 表头和表盘用
|分隔
Q2: 条件匹配不准确?
A:
- 检查物料的选择条件表达式是否正确
- 运行
Test3_ConditionMatcher验证匹配器功能 - 使用
TestExpression方法测试具体表达式
Q3: 如何调试条件提取?
A: 使用以下代码查看提取结果:
Dim conditions As Object
Set conditions = bomMgr.ParseModelAndExtractConditions(modelStr)
Dim key As Variant
For Each key In conditions.Keys
Debug.Print key & " = " & conditions(key)
Next key
Q4: 批量处理速度慢?
A:
- 避免在循环中重复加载BOM数据
- 使用
GetMaterialsByModel一次性获取所有类别 - 考虑添加缓存机制
Q5: 如何验证物料条件表达式?
A: 运行示例:
Example5_ValidateMaterialConditions
这将生成一个验证报告,显示所有物料条件的有效性。
测试用例
运行所有测试
RunAllTests
单独测试
' 测试型号解析
Test1_ModelParser
' 测试条件提取
Test2_ConditionExtractor
' 测试条件匹配
Test3_ConditionMatcher
' 测试完整流程
Test4_GetMaterialsByModel
应用示例
示例1: 生成领料清单
Example1_GeneratePickingListByModel
示例2: 批量处理型号
Example2_BatchProcessModels
示例3: 查询型号详情
Example3_ShowModelDetails
示例4: 对比两个型号
Example4_CompareModels
示例5: 验证条件表达式
Example5_ValidateMaterialConditions
技术支持
如有问题,请:
- 查看测试结果和调试信息
- 运行相关示例程序
- 检查条件表达式语法
文档版本: v1.0
最后更新: 2026-01-19