组件拉伸时不拉伸Connector:实例化时能否添加图形标注?
Absolutely! You can control the scaling behavior of connectors or components in Modelica using graphical annotations—this is exactly the kind of tweak we use to keep diagrams clean and visually consistent, even when components are resized. Let’s break down how to fix this for your Modelica.Blocks.Sources.RealExpression example and other cases:
1. Fix Connector Size Directly When Instantiating
If you want to lock down the size of a specific connector (like an input/output port), you can override its icon annotation and set the fixedSize=true property on its graphical elements. For example, instantiating a fixed-size Real input connector:
Modelica.Blocks.Interfaces.RealInput myFixedInput annotation( Icon(graphics={ Rectangle( extent={{-10,-10},{10,10}}, lineColor={0,0,255}, fillColor={191,219,255}, fillPattern=FillPattern.Solid, fixedSize=true // This locks the rectangle to its original size ) }) );
The fixedSize=true flag tells the Modelica editor to ignore any component scaling for this specific graphical element—so the connector will stay the same size no matter how you stretch the parent component.
2. Lock Entire Component Icon (Like RealExpression)
For pre-built components such as Modelica.Blocks.Sources.RealExpression, you can override the component’s root annotation to fix its entire icon size. This prevents the entire component (including its connectors) from stretching when you resize it:
Modelica.Blocks.Sources.RealExpression myExpr(expression=sin(time)) annotation( Icon(fixedSize=true) // Locks the entire component icon to its default size );
This is the quickest fix for your specific example—after adding this annotation, stretching the myExpr component won’t distort its icon or connectors at all.
3. Fine-Grained Control (Mix Scalable and Fixed Elements)
If you want part of the component to scale but keep connectors fixed, you can apply fixedSize=true only to the connector graphics in the component’s annotation. For custom components, this would look like:
model CustomComponent Modelica.Blocks.Interfaces.RealInput u annotation( Icon(graphics={Rectangle(extent={{-10,-10},{10,10}}, fixedSize=true)}) ); Modelica.Blocks.Interfaces.RealOutput y; equation y = u*2; annotation( Icon(graphics={ // Scalable background rectangle Rectangle(extent={{-50,-20},{50,20}}, fillColor={240,240,240}), // Fixed-size text label Text(text="Custom", fixedSize=true) }) ); end CustomComponent;
Here, the background rectangle scales with the component, but the input connector and text label stay fixed.
Key Notes
- The
fixedSizeproperty works on all graphical elements (rectangles, circles, text, lines) and can be applied to individual elements or entireIcon/Diagramblocks. - Most Modelica editors (like Dymola, OpenModelica) respect this annotation—your connectors will stay crisp and consistent no matter how you adjust component sizes.
内容的提问来源于stack exchange,提问作者Rene Just Nielsen

