Загрузка данных
Write a complete VBA macro for SOLIDWORKS (.swp).
The macro is created through Tools -> Macro -> New and executed through Tools -> Macro -> Run.
The entry point must be:
Sub main()
CODE REQUIREMENTS
1. Entire source code must contain ASCII characters only.
2. No Russian/Cyrillic characters in code, comments, strings, variable names, or procedure names.
3. Use late binding only.
4. All SOLIDWORKS objects must be declared As Object.
5. Use:
Set swApp = Application.SldWorks
6. No early binding.
7. No SOLIDWORKS type library references or named API constants.
8. The only public entry procedure is:
Sub main()
9. All other procedures and functions must be Private.
10. The final answer must contain the complete compilable VBA code without omissions or "...".
11. The code must compile in the SOLIDWORKS VBA editor using Debug -> Compile.
MODEL STRUCTURE
The model structure is:
Top assembly
-> selected subassembly
-> parts
Example part names may be Profile, Clamp, Cover, but names are arbitrary.
Do not hardcode component names.
All direct child parts of the selected subassembly must be processed generically.
USER SELECTION
In the top-level assembly the user Ctrl-selects:
1. A dimension text in the top-level assembly.
2. The required subassembly component in FeatureManager.
Use SelectionManager:
Set swSel = swTop.SelectionManager
cnt = swSel.GetSelectedObjectCount2(-1)
Use:
t = swSel.GetSelectedObjectType3(i, -1)
Set o = swSel.GetSelectedObject6(i, -1)
Selection types:
20 = component
14 = dimension
For the selected dimension:
Set dd = o.GetDimension
L = dd.SystemValue * 1000
SOLIDWORKS API dimensions are in meters.
Read:
dimension.SystemValue * 1000
to obtain millimeters.
Write:
valMM / 1000
to set meters.
MAIN TASK
If the selected top-level dimension is, for example:
4400 mm
the selected subassembly and every direct child part must receive a configuration:
L_4400
and the controlling dimension:
D1@LENGHT
must be set to:
4400 mm
inside that configuration.
The same selected length L is intentionally used for the subassembly and all direct child parts.
Do NOT determine the target part length by reading the old D1@LENGHT value of the part.
For example:
Selected top dimension = 4400
Result:
Selected subassembly:
configuration = L_4400
D1@LENGHT = 4400
Part 1:
configuration = L_4400
D1@LENGHT = 4400
Part 2:
configuration = L_4400
D1@LENGHT = 4400
Part 3:
configuration = L_4400
D1@LENGHT = 4400
CONFIGURATION RULES
Configuration name:
"L_" & CStr(CLng(L))
Example:
4400 mm -> L_4400
If the configuration already exists:
- do not create a duplicate;
- activate and reuse the existing configuration;
- make sure D1@LENGHT has the requested value.
If it does not exist:
- create it.
First try:
m.AddConfiguration3(cfg, "", "", 0)
If that returns Nothing, fallback to:
m.ConfigurationManager.AddConfiguration2(cfg, "", "", 0, "", "")
CRITICAL CONFIGURATION ISOLATION
Existing configurations must retain their own dimensions.
Example:
L_1000 must remain 1000
L_3333 must remain 3333
L_4400 must remain 4400
Changing or creating L_4400 must not overwrite D1@LENGHT in L_1000 or L_3333.
The selected subassembly component in the top-level assembly must be switched to L_4400.
Other instances of the same subassembly file in the top-level assembly must NOT have their ReferencedConfiguration changed.
Only modify:
comp.ReferencedConfiguration = cfgName
for the component selected by the user.
LENGTH DIMENSION
All relevant subassemblies and parts use the controlling dimension:
D1@LENGHT
The spelling LENGHT is intentional.
FindLenDim must primarily use:
m.Parameter("D1@LENGHT")
Optionally fallback to:
m.Parameter("D1@LENGTH")
Do not determine the target part length from its old dimension.
The target length for every part is the selected top-level value L.
SUBASSEMBLY PROCESS
1. Get selected subassembly:
Set swSub = comp.GetModelDoc2
2. It must be loaded.
3. It must have GetType = 2.
4. Remember the currently active document configuration.
5. Build:
cfgName = "L_" & CStr(CLng(L))
6. Create or reuse cfgName.
7. Activate cfgName.
8. Find D1@LENGHT.
9. Set D1@LENGHT = L only for the intended configuration.
10. Rebuild the subassembly.
11. Verify that the dimension equals L within 0.05 mm.
12. Save the subassembly.
13. Set only the selected top-level component:
comp.ReferencedConfiguration = cfgName
14. Rebuild and save the top-level assembly.
PART PROCESS
Use:
v = swSub.GetComponents(False)
Process only direct child components.
For each component:
- skip Nothing;
- skip suppressed components;
- if GetModelDoc2 returns Nothing, report NOT LOADED;
- process only documents where GetType = 1;
- if GetType = 2, report NESTED SUBASSEMBLY - SKIPPED.
For every part:
1. Remember its currently active document configuration.
2. Create or reuse the same cfgName:
L_<selected top-level length>
3. Activate cfgName.
4. Find:
D1@LENGHT
5. Set:
D1@LENGHT = L
6. Rebuild.
7. Verify the actual value.
8. Save the part.
9. Set the part component:
ch.ReferencedConfiguration = cfgName
10. Restore the previously active document configuration if required, without changing ch.ReferencedConfiguration.
11. Add the actual verified length to the final report.
SAVE REQUIREMENT
Save3 must use ByRef variables.
Always declare:
Dim e As Long
Dim w As Long
Use:
e = 0
w = 0
m.Save3 1, e, w
Never call Save3 with literal error/warning arguments.
PERFORMANCE
Use status-bar progress.
Do not use ForceRebuild3.
Use EditRebuild3.
Status examples:
STEP 1/5: reading selection...
STEP 2/5: subassembly L_4400...
STEP 3/5: rebuild top assembly...
STEP 4/5: processing parts...
STEP 4/5: <component name>...
STEP 5/5: final rebuild + save...
READY
FINAL PROCESS
After all parts:
1. Activate cfgName in the subassembly.
2. Rebuild the subassembly.
3. Save the subassembly.
4. Make sure the selected top-level component still references cfgName.
5. Rebuild the top-level assembly.
6. Save the top-level assembly.
7. Restore the previously active subassembly document configuration if desired.
8. Do NOT change the selected component away from cfgName.
FINAL REPORT
Show a MsgBox such as:
DONE L_4400
SUB : 4400 mm -> L_4400
Profile-1 : 4400 mm -> L_4400
Clamp-1 : 4400 mm -> L_4400
Cover-1 : 4400 mm -> L_4400
The report must use actual verified D1@LENGHT values after setting the dimensions.
ERROR HANDLING
main must start with:
On Error GoTo EH
At the end:
EH:
MsgBox "ERROR: " & Err.Description
Use On Error Resume Next only locally around API operations that may fail harmlessly.
HELPER PROCEDURES
Use:
Sub main()
Private Function FindLenDim(m As Object) As Object
Private Function EnsureConfig(m As Object, cfg As String) As Object
Private Function SetLengthThisConfig(m As Object, d As Object, valMM As Double, cfg As String) As Boolean
Private Function FormatMM(v As Double) As String
Private Sub Status(m As Object, txt As String)
OUTPUT
Return one complete VBA code block.
Do not truncate the code.
Do not use "...".
Do not return partial patches.
The code must be directly pasteable into a normal SOLIDWORKS VBA module.